NHibernate second-level caching - evicting regions

后端 未结 2 1775
逝去的感伤
逝去的感伤 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:22

    OK, looks like i've answered my own question. The default interface that's returned when you pull out the nHibernate metadata doesn't provide information on caching, however if you dig around in the implementations of it, it does. A bit clumsy, but it does the job.

    0 讨论(0)
  • 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<ISessionFactory>();
    
            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);
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题