Adr*_*Pop 11 java ehcache playframework-2.4
建立:
我已经按照https://www.playframework.com/documentation/2.4.0/JavaCache上的手册来分离缓存并使用我在application.conf中配置的不同配置(缓存大小,生命周期等):
play.cache.bindCaches = ["mycache1-cache","mycache2-cache"]
Run Code Online (Sandbox Code Playgroud)
然后,为了配置它们,我创建了通常的ehcache.xml文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
<defaultCache
maxBytesLocalHeap="256000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="10000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="mycache1-cache"
maxBytesLocalHeap="256000000"
eternal="false"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
overflowToDisk="true"
maxElementsOnDisk="1000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
Run Code Online (Sandbox Code Playgroud)
它只在我保留defaultCache时有效,但是一旦我添加了自定义缓存,就会抛出以下内容:
ProvisionException:无法配置,请参阅以下错误:1)自定义提供程序中的错误net.sf.ehcache.ObjectExistsException:缓存mycache1-cache已存在
但是,如果我只在ehcache.xml中定义缓存而不是在application.conf中定义缓存,则play不知道它并抛出.
小智 1
一个月前我也遇到了同样的问题,并尝试通过互联网搜索以下适合我的解决方案。您也可以尝试这样做。
/** Bind named caches using configuration in ehcache.xml. Define a named cache in ehcache.xml and Ehcache will automatically instantiate the cache at runtime. However,this cache is unknown to the Play cache plugin and is not accessible using {@code @NamedCache}. You must bind a reference to the named cache using {@link CacheModule#bindCustomCache(String)}. Do not add the named cache to {@code play.cache.bindCaches} in configuration. Caches bound in this manner are programmatically added by the Play cache plugin at runtime. The cache plugin will always copy settings from the default cache, limiting the usefulness of named caches. **/
public class CacheModule extends AbstractModule {
@Override
protected void configure() {
bindCustomCache("tenants");
bindCustomCache("user-agent");
bindCustomCache("geo-ip");
bindCustomCache("full-contact");
}
/**
* Bind a named cache that is defined in ehcache.xml.
*
* @param name The name of the cache.
*/
private void bindCustomCache(String name) {
bind(CacheApi.class)
.annotatedWith(new NamedCacheImpl(name))
.toProvider(new Provider<CacheApi>() {
@Inject
CacheManager cacheManager;
@Override
public CacheApi get() {
Cache cache = cacheManager.getCache(name);
if (cache == null) {
throw new RuntimeException("Cache named '" + name + "' must be defined in ehcache.xml");
}
return new DefaultCacheApi(new EhCacheApi(cache));
}
})
.asEagerSingleton();
}
}
Run Code Online (Sandbox Code Playgroud)
我在 ehcache.xml 中创建命名缓存,只需在配置方法中添加一行 bindCustomCache() 即可。
| 归档时间: |
|
| 查看次数: |
1372 次 |
| 最近记录: |