从 NHibernate 二级缓存中删除对象
我刚刚开始考虑在我的一个应用程序中使用 NHibernate 二级缓存。我可能会使用依赖于 ASP.net 缓存的 NHibernate.Caches.SysCache.SysCacheProvider
。
启用缓存不是问题,但我想知道如何管理缓存,例如以编程方式从缓存中删除某些实体等。
我的应用程序是某种图像数据库。用户通过后端上传图像,并可以通过访问 /ImageDb/Show?userId=someUserId
在前端查看图像。
数据不会经常更改。如果它发生变化,用户不会关心后端中名为“清除我的缓存”的按钮,该按钮会从缓存中删除该用户的缓存对象。
我在网上找到了一个解决方案,可以从 nhibernates 二级缓存中删除所有缓存的对象。但这对我来说有点太暴力了......我不想仅仅因为一个用户试图清除自己数据的缓存而清除数十个用户的整个缓存。
所以我基本上想做的是:有选择地从 C# 中的 nhibernates 二级缓存中删除缓存的数据库对象。
这可能吗?我想这也取决于缓存提供者。如果这不能通过 ASP.net 缓存提供程序实现,我愿意接受其他内置/开源建议。
I just started thinking about using the NHibernate second level cache in one of my apps. I would probably use the NHibernate.Caches.SysCache.SysCacheProvider
which relies on ASP.net cache.
Enabling the cache was not a problem, but I am wondering on how to manage the cache e. g. programmatically removing certain entities from the cache etc.
My application is some kind of image database. The user uploads images over a backend and can view it in the frontend by accessing /ImageDb/Show?userId=someUserId
The data does not change very often. And if it changes, the users would not matter a button named "clear my cache" in the backend that removes the cached objects for this user from the cache.
I found a solution online that can remove all cached objects from nhibernates second level cache. But thats a bit too brute force for me ... I dont want to clear the whole cache for dozens of users just because one user tried to clear the cache for his own data.
So what I basically wanted to do: selectively remove cached db objects from nhibernates second level cache in C#.
Is this possible? I guess it also depends on the cache provider. If this is not doable with the ASP.net cache provider, I am open for other built in / open source suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sessionFactory 提供了你想要的方法......
来自 NHibernate 参考的 19.3 章节:
完全驱逐所有会话缓存中的对象,调用 ISession.Clear()
对于二级缓存,ISessionFactory 上定义了一些方法,用于逐出某个对象的缓存状态。
实例、整个类、集合实例或整个集合角色。
the sessionFactory provides the methods you want...
from the 19.3 chapter of the NHibernate reference:
To completely evict all objects from the session cache, call ISession.Clear()
For the second-level cache, there are methods defined on ISessionFactory for evicting the cached state of an
instance, entire class, collection instance or entire collection role.