NHibernate 二级缓存——驱逐区域 [英] NHibernate second-level caching - evicting regions

查看:45
本文介绍了NHibernate 二级缓存——驱逐区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 nHibernate 实现中设置了许多缓存区域.为了避免负载平衡的 Web 服务器出现问题,我想有效地禁用编辑缓存数据的页面上的缓存.我可以编写一个方法来轻松清除所有查询缓存、类缓存和实体缓存.

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 caching on the pages that edit the cached data. I can write a method that clears out all my query caches, my class caches and my entity caches easily enough.

但我真正想要的是按区域清除缓存.sessionFactory.EvictQueries() 将采用区域参数,但 Evict() 和 EvictCollection() 不会.我真的不想在这里扔掉整个缓存,也不想维护某种笨拙的字典,将类型与其缓存区域相关联.nHibernate 是否可以询问实体或集合的缓存设置是什么?

But what I really want is to clear the cache by region. sessionFactory.EvictQueries() will take a region parameter, but Evict() and EvictCollection() does not. I don't really want to throw away the whole cache here, nor do I want to maintain some sort of clumsy dictionary associating types with their cache regions. Does nHibernate have a way to ask an entity or collection what its caching settings are?

谢谢

推荐答案

我刚刚做了同样的事情.为了大家的利益,这里是我构建的方法:

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);
                }
            }
        }
    }

这篇关于NHibernate 二级缓存——驱逐区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆