【问题标题】:Ehcache Cache Server + BlockingCache ?Ehcache 缓存服务器 + BlockingCache ?
【发布时间】:2012-02-11 04:34:21
【问题描述】:

是否可以使用 Ehcache 缓存服务器并将其配置为 blockingCache ?我似乎无法在 ehcache.xml 文件中找到如何配置它......只能以编程方式。

【问题讨论】:

    标签: ehcache


    【解决方案1】:

    要通过 ehcache.xml 使用 BlockingCache 作为缓存的默认装饰器,首先你应该实现自己的 CacheDecoratorFactory,比如说它是 DefaultCacheDecoratorFactory:

    public class DefaultCacheDecoratorFactory extends CacheDecoratorFactory {
        @Override
        public Ehcache createDecoratedEhcache(Ehcache cache, Properties properties) {
            return new BlockingCache(cache);
        }
    
        @Override
        public Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties) {
            return new BlockingCache(cache);
        }
    }
    

    然后将其配置为缓存定义的一部分,如下所示:

    <cache name="CACHE_NAME" more values here.../>
        <cacheDecoratorFactory class="whatsoever.DefaultCacheDecoratorFactory"/>
    </cache>
    

    并且使用 cacheManager.getEhCache() 来访问 cacheManager.getCache() 以外的缓存,因为它只为您的装饰缓存返回 null。

    【讨论】:

      【解决方案2】:

      您可以通过编程方式声明修饰缓存,也可以在配置中声明,请参阅: http://ehcache.org/documentation/apis/cache-decorators#by-configuration

      您需要添加一个 net.sf.ehcache.constructs.CacheDecoratorFactory 实现来满足您的需求。我猜在你的情况下,它可以对传递给 net.sf.ehcache.constructs.CacheDecoratorFactory#createDecoratedEhcache 的 Ehcache 实例进行一些模式匹配,并返回 null 或由 BlockingCache 修饰的缓存实例。

      但要注意的是,确保在未命中时,始终放回(甚至为 null)缓存,否则该键/段的写锁定将不会被解锁。

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 2016-05-05
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多