2012-02-11 156 views

回答

1

你可以声明编程装饰缓存,而且在配置,请参阅: http://ehcache.org/documentation/apis/cache-decorators#by-configuration

你需要添加一个net.sf.ehcache.constructs.CacheDecoratorFactory实现,做你所需要的。我想你可能会对传递给net.sf.ehcache.constructs.CacheDecoratorFactory#createDecoratedEhcache的Ehcache实例进行一些模式匹配,并返回null或由BlockingCache装饰的缓存实例。

尽管如此,谨慎的做法是确保在未命中时,您总是在处将放回缓存中,否则将不会解锁该密钥/段的写入锁定。

6

要使用BlockingCache为通过ehcache.xml中缓存的缺省装饰,首先应该实现自己的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您的装修缓存。