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

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

For example, I save a user-selected store item under the next session key "currentItem". In the website management area, I decided to delete this store item. Now I want to invalidate all currentItem cache entries in all sessions.

Is there any way to do this?

I understand that the example I gave is a bit contrived, but it makes sense. I suspect that one solution would be to use a regular api cache to store all current user elements. That way, I could cancel them if necessary.

thanks

+7
source share
2 answers

You cannot get all current sessions from all users.
(Honestly, you can , but I do not recommend this, and yet you can only read InProc sessions this way).

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

  • Store only Session ID user's basket
  • Add item with corresponding ID to cache
  • Then adding the item to Cache , set the dependency to the item
  • Then just raise an event to change entire carts in Cache .
  • You should also consider re-creating the elements, if for some reason Cache freed, you must provide the CacheItemRemovedCallback callback for this event.
+3
source

You can move the shopping cart to the database. Unless you have a very fast buying process or an extremely redundant environment, the session or cache probably will not be strong enough. Since they are not saved, you will also not have a direct method for analyzing purchase decisions, depletion, etc.

Depending on how often your inventory changes, you may only need to verify that the cart item is available at checkout. As a user, it would be very strange for me to see that the item simply β€œmagically” disappeared from my cart or that the price changed. In my opinion, it would be better to directly call it.

Once again, the database would be an excellent solution, since you could track the state of an item when it was added to the cart against the current product definition.

If you must use the Session or Cache solution, then the proposed @VMAtm solution is valid.

0
source

All Articles