Reputation: 26
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.
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.
Also, 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.
Upvotes: 0
Views: 320
Reputation: 1935
Clear 2-level cache nHibernate (this method worked for me):
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);
}
Upvotes: 0