是否可以使用Ehcache缓存服务器并将其配置为blockingCache?我似乎找不到如何在ehcache.xml文件中配置此选项...只能通过编程实现。
是否可以使用Ehcache缓存服务器并将其配置为blockingCache?我似乎找不到如何在ehcache.xml文件中配置此选项...只能通过编程实现。
要通过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。
您可以在程序中声明装饰缓存,也可以在配置中进行声明,参见: http://ehcache.org/documentation/apis/cache-decorators#by-configuration
您需要添加一个net.sf.ehcache.constructs.CacheDecoratorFactory实现,以执行您所需的操作。我猜在您的情况下,它可以针对传递给net.sf.ehcache.constructs.CacheDecoratorFactory#createDecoratedEhcache的Ehcache实例进行一些模式匹配,并返回null或由BlockingCache装饰的缓存实例。
但请注意,确保在未命中时,您始终将(甚至是null)放回缓存中,否则该键/段的写锁定将无法解锁。