2012-02-11 151 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您的裝修緩存。