NHibernate second-level caching - evicting regions

后端 未结 2 1774
逝去的感伤
逝去的感伤 2021-01-06 00:47

We have a number of cache regions set up in our nHibernate implementation. In order to avoid trouble with load balanced web servers, I want to effectively disable the cachin

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 01:24

    I've just done the same thing. For everyone's benefit, here is the method I constructed:

    public void ClearCache(string regionName)
        {
            // Use your favourite IOC to get to the session factory
            var sessionFactory = ObjectFactory.GetInstance();
    
            sessionFactory.EvictQueries(regionName);
    
            foreach (var collectionMetaData in sessionFactory.GetAllCollectionMetadata().Values)
            {
                var collectionPersister = collectionMetaData as NHibernate.Persister.Collection.ICollectionPersister;
                if (collectionPersister != null)
                {
                    if ((collectionPersister.Cache != null) && (collectionPersister.Cache.RegionName == regionName))
                    {
                        sessionFactory.EvictCollection(collectionPersister.Role);
                    }
                }
            }
    
            foreach (var classMetaData in sessionFactory.GetAllClassMetadata().Values)
            {
                var entityPersister = classMetaData as NHibernate.Persister.Entity.IEntityPersister;
                if (entityPersister != null)
                {
                    if ((entityPersister.Cache != null) && (entityPersister.Cache.RegionName == regionName))
                    {
                        sessionFactory.EvictEntity(entityPersister.EntityName);
                    }
                }
            }
        }
    

提交回复
热议问题