For example, I have some cached items with same prefix, such as
'app_111111', 'app_222222', 'app_333333', ...
Can I remove such 'app_xxxxxx' items by any memcached commands?
Memcached flush_all command is used to delete all data (key-value pairs) from the Memcached server. It accepts an optional parameter called time that sets a time after which the Memcached data is to be cleared.
Memcached::delete() deletes the key from the server. The time parameter is the amount of time in seconds (or Unix time until which) the client wishes the server to refuse add and replace commands for this key.
Memcached does not offer this functionality out of the box so you have to build it in yourself.
The way I solve this is by defining a prefix (or namespace) in my application for groups of keys. Any key that I set in memcached has that prefix before it. Whenever I want to "delete" stuff from Memcached, I just change the prefix. And whenever I want to lookup a key in Memcached, I add that prefix to it.
In your case, you could start by setting the prefix to, say, MyAppPrefix1
, so your keys will be stored as MyAppPrefix1::app_333333
, MyAppPrefix1::app_444444
.
Later on when you want to "delete" these entries, set your application to use MyAppPrefix2
. Then, when you try to get a key from Memcached called app_333333
, it will look for MyAppPrefix2::app_333333
and will not find it the first time around, as if it had been deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With