Java 指定全局缓存容量
我正在尝试将我的项目代码从OSCache迁移到EhCache 我们使用OSCache不仅作为第二级Hibernate缓存提供程序,而且还存储其他不同性质的对象。它们都愉快地共享同一个缓存实例,而不会因为缓存键不重叠而发生任何冲突 当移向EhCache时,一个很大的区别是每个区域都有其不同的缓存实例。这可能很好,因为它可以提高查找速度,因为不同性质的数据分别驻留。不幸的是,这要付出地狱般的代价。让我解释一下 在OSCache世界中,我会将缓存容量配置为10000。现在,如果一个特定的安装需要/能够负担得起更多的RAM,我可以轻松地将其增加到50000,这样就可以了。现在在EhCache中,我必须按每个区域的增量部分更改设置 此外,一个安装可能具有更高的X类型对象使用率,而另一个安装可能更喜欢更高的Y类型对象使用率。我们有几十个安装,每个安装都有数百个不同的缓存。为此,我们将不得不雇佣一群人,他们什么也不做,只是监视缓存模式和调整设置 我希望Java 指定全局缓存容量,java,hibernate,caching,ehcache,oscache,Java,Hibernate,Caching,Ehcache,Oscache,我正在尝试将我的项目代码从OSCache迁移到EhCache 我们使用OSCache不仅作为第二级Hibernate缓存提供程序,而且还存储其他不同性质的对象。它们都愉快地共享同一个缓存实例,而不会因为缓存键不重叠而发生任何冲突 当移向EhCache时,一个很大的区别是每个区域都有其不同的缓存实例。这可能很好,因为它可以提高查找速度,因为不同性质的数据分别驻留。不幸的是,这要付出地狱般的代价。让我解释一下 在OSCache世界中,我会将缓存容量配置为10000。现在,如果一个特定的安装需要/能够
CacheManager
具有某种全局缓存容量设置,每个内部缓存都会根据条目使用情况争取更多的容量。但是,我发现设置缓存容量的唯一方法是通过CacheConfiguration
,它与CacheManager
相比是多对一的
到目前为止,我能看到的唯一选择是尝试强制Hibernate对所有实体使用一个全局缓存。有人知道怎么做吗?对于我的场景,还有其他更好的解决方案吗?您可以尝试使用一个缓存并在其周围添加装饰器。装饰器的名称可以与您的区域名称相匹配,这样hibernate就可以使用这些缓存,但这些装饰器将在下面使用相同的缓存。因此,只有一个缓存配置需要管理。 您可以通过实现自定义和设置装饰缓存的名称来实现这一点 您可以使用类似以下内容的ehcache.xml:
“com.xyz.util.CustomEhcacheDecoratorFactory”是一个自定义ehcache装饰器工厂类,用于创建修饰的ehcache。您可以使用“properties”属性以任何方式设置修饰的ehcache,这里您只使用name属性来配置新修饰的ehcache的名称。所有其他操作都可以委托给基础缓存
这里提供了一个适用于此用例的定制缓存装饰器,它重用了ehcache jar中的EhcacheDecoratorAdapter,只覆盖getName()。EhcacheDecoratorAdapter将所有操作委托给在构造函数中传递的基础ehcache:
package com.xyz.util;
import java.util.Properties;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.constructs.CacheDecoratorFactory;
import net.sf.ehcache.constructs.EhcacheDecoratorAdapter;
public class CustomEhcacheDecoratorFactory extends CacheDecoratorFactory {
public Ehcache createDecoratedEhcache(final Ehcache cache,
final Properties properties) {
return new EhcacheDecoratorAdapter(cache) {
private final String name = properties.getProperty("name");
public String getName() {
return name;
}
};
}
public Ehcache createDefaultDecoratedEhcache(final Ehcache cache,
final Properties properties) {
return new EhcacheDecoratorAdapter(cache) {
private final String name = properties.getProperty("name");
public String getName() {
return name;
}
};
}
}
包com.xyz.util;
导入java.util.Properties;
导入net.sf.ehcache.ehcache;
导入net.sf.ehcache.constructs.cachedecorfactory;
导入net.sf.ehcache.constructs.ehcachedecoradapter;
公共类CustomeCacheDecoratorFactory扩展了CacheDecoratorFactory{
公共Ehcache createDecoratedEhcache(最终Ehcache缓存,
最终属性(属性){
返回新的EhcacheDecoratorAdapter(缓存){
私有最终字符串名称=properties.getProperty(“名称”);
公共字符串getName(){
返回名称;
}
};
}
公共Ehcache createDefaultDecoratedEhcache(最终Ehcache缓存,
最终属性(属性){
返回新的EhcacheDecoratorAdapter(缓存){
私有最终字符串名称=properties.getProperty(“名称”);
公共字符串getName(){
返回名称;
}
};
}
}
哇,非常感谢你的回答,圣诞节后我一定会看一看。 package com.xyz.util; import java.util.Properties; import net.sf.ehcache.Ehcache; import net.sf.ehcache.constructs.CacheDecoratorFactory; import net.sf.ehcache.constructs.EhcacheDecoratorAdapter; public class CustomEhcacheDecoratorFactory extends CacheDecoratorFactory { public Ehcache createDecoratedEhcache(final Ehcache cache, final Properties properties) { return new EhcacheDecoratorAdapter(cache) { private final String name = properties.getProperty("name"); public String getName() { return name; } }; } public Ehcache createDefaultDecoratedEhcache(final Ehcache cache, final Properties properties) { return new EhcacheDecoratorAdapter(cache) { private final String name = properties.getProperty("name"); public String getName() { return name; } }; } }