简体   繁体   English

有没有办法清除所有用户的会话缓存密钥?

[英]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".例如,我将用户当前选择的商店商品存储在以下会话键“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.我现在想使所有会话中的所有“currentItem”缓存条目无效。

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.我怀疑一种解决方案是使用普通的 Cache api 来存储所有用户的当前项目。 That way I could invalidate them when required.这样我就可以在需要时使它们无效。

thanks谢谢

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). (坦率地说, 您可以,但我不建议这样做,而且您仍然只能通过这种方式读取InProc sessions )。

So the only thing you can do is something like this:所以你唯一能做的就是这样:

  • Store in Session only ID of the user cart仅在Session存储用户购物车的ID
  • Add item with corresponding ID to the Cache将对应ID item添加到Cache中
  • Then adding the item to the Cache , set the dependency to the Item然后将 item 添加到Cache ,将依赖设置为 Item
  • Then just generate event to change whole carts in Cache .然后只需生成事件来更改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.此外,如果由于某些原因Cache被清空,您应该考虑重新创建项目 - 您应该为此事件提供CacheItemRemovedCallback回调

You could move the shopping cart to the database.您可以将购物车移至数据库。 Unless you have a very rapid buying process, or an extremely redundant environment, Session or Cache will probably not be durable enough.除非您的购买过程非常快,或者环境极其冗余,否则 Session 或 Cache 可能不够耐用。 Since they are not persisted, you also won't have a direct method to analyze buying decisions, attrition, etc.由于它们不是持久化的,因此您也没有直接的方法来分析购买决策、损耗等。

Depending on how often your inventory changes, you might only need to validate that a cart item is available at checkout.根据您的库存更改频率,您可能只需要在结账时验证购物车项目是否可用。 As a user, it would be very confusing to me to see that an item had just "magically" disappeared from my cart or that the price had changed.作为用户,看到某件商品刚刚“神奇地”从我的购物车中消失或价格发生了变化,我会感到非常困惑。 It would be better to explicitly call this out, in my opinion.在我看来,最好明确指出这一点。

Again, a database would be a superior solution as you could track the state of the item at the time it was added to the cart against the current definition of the product.同样,数据库将是一个更好的解决方案,因为您可以根据产品的当前定义跟踪将项目添加到购物车时的状态。

If you must use a Session or Cache-based solution, then @VMAtm's proposed solution is a valid option.如果您必须使用基于会话或缓存的解决方案,那么@VMAtm 提出的解决方案是一个有效的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM