Reputation: 607
First, my purpose is when the user close the browser then the user session data should expire. Now the problem is that, my Server requires memcached to work properly. So, I want to delete that specific user session from memcached who has closed his browser. I dont want to clear all memcached so that the remaining users' session should still exist.
Upvotes: 3
Views: 3090
Reputation: 176472
In Memcached there is no way to list all keys in a specific namespace unless you want to use some workarounds, and I don't recommend you in production.
One alternative would be to store all user-related Memcached keys in a set (stored in memcached as well). The set key is generated using the user id, so that you always know in advance its value and you can easily retrieve it.
Once the user session ends, you can fetch the item containing the list of user keys, then delete each key. Keep in mind that the key containing the list of keys should have an expiration greater than any item-specific item.
It is also worth mentioning that Redis supports key iteration and sets by default, and you can configure it to work like Memcached. You may want to give it a try.
Upvotes: 2