[英]Evict 2nd level cache items Syscache/Nhibernate
I am using NHibernate 3.3 with Syscache 2nd level cache and I am trying to evict an object from the 2nd level cache, but it doesn't seem to work. 我正在将NHibernate 3.3与Syscache 2级缓存一起使用,并且试图从2级缓存中逐出一个对象,但是它似乎不起作用。
I am using this code based on other examples: 我正在基于其他示例使用此代码:
var CollectionCache = NHibernateSessionFactory.Instance.GetAllCollectionMetadata();
var ClassCache = NhibernateSessionFactory.Instance.GetAllClassMetadata();
NhibernateSessionFactory.Instance.EvictQueries();
foreach (var collectionMetadata in NhibernateSessionFactory.Instance.GetAllCollectionMetadata())
NhibernateSessionFactory.Instance.EvictCollection(collectionMetadata.Key);
foreach (var classMetadata in NhibernateSessionFactory.Instance.GetAllClassMetadata())
NhibernateSessionFactory.Instance.EvictEntity(classMetadata.Key);
After evicting, the CollectionCache and ClassCache still have the same number of items. 逐出后,CollectionCache和ClassCache仍具有相同数量的项目。
Also, NhibernateSessionFactory.Instance.Evict(typeof(someObject), SomeObjectId);
另外, NhibernateSessionFactory.Instance.Evict(typeof(someObject), SomeObjectId);
does not seem to have any effect. 似乎没有任何作用。
My use case: I am working with detached objects, and when another application makes changes to the database, I need to evict those items from the 2nd level cache to keep in it in sync. 我的用例:我正在处理分离的对象,并且当另一个应用程序对数据库进行更改时,我需要从第二级缓存中逐出这些项以使其保持同步。
Clear 2-level cache nHibernate (this method worked for me): 清除2级缓存nHibernate(此方法对我有用):
private void ClearCache()
{
_repositoryFactory.GetSession().Clear();
var sf = _repositoryFactory.GetSession().SessionFactory;
sf.EvictQueries();
foreach (var collectionMetadata in sf.GetAllCollectionMetadata())
sf.EvictCollection(collectionMetadata.Key);
foreach (var classMetadata in sf.GetAllClassMetadata())
sf.EvictEntity(classMetadata.Key);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.