Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to clear a session cache key for all users?

Is there a way to clear a session cache key for all users?

For example, I am storing the users currently selected shop item under the following session key "currentItem". In the management area of the website, I decide to delete that particular shop item. I now want to invalidate all the "currentItem" cache entries across all sessions.

Is there a way to do this?

I realise the example I have given is a little contrived, but it gets the point across. I suspect one solution would be to use the normal Cache api to store all of the users current items. That way I could invalidate them when required.

thanks

like image 592
gregpakes Avatar asked Feb 02 '12 09:02

gregpakes


People also ask

Does clearing cache clear session?

The standard mechanism for tracking sessions is a cookie. Clearing your browser's cache, and therefore your cookies as well, will result in no session ID being sent to the server when a request is made, so it has no choice but to begin a new session.

Can we store session in cache?

It's possible to use Redis Enterprise as both a cache and a session store in a single setup, as shown in the picture below. In this design, the application layer accesses and maintains the data in the cache. Therefore, all the sessions running inside the application access the same data stored in the cache.


1 Answers

You can't get all the current Sessions from all users
(Frankly speaking, you can, but I don't recommend this, and still you can read only InProc sessions this way).

So the only thing you can do is something like this:

  • Store in Session only ID of the user cart
  • Add item with corresponding ID to the Cache
  • Then adding the item to the Cache, set the dependency to the Item
  • Then just generate event to change whole carts in Cache.
  • Also you should think about re-creating items if for some reasons Cache is being emptied - you should provide the CacheItemRemovedCallback callback for this event.
like image 189
VMAtm Avatar answered Nov 15 '22 09:11

VMAtm