我如何像休眠缓存提供程序一样集成cache2k?
我的意思是在当前项目中,我们使用ehcache和
在下一个配置hibernate.cfg.xml中启用缓存:
<hibernate-configuration>
<session-factory>
<!-- Cache Configurations -->
<!-- Using net.sf.ehcache.hibernate.SingletonEhCacheProvider instead of
net.sf.ehcache.hibernate.EhCacheProvider ensures the same instance
of CacheManager is referred to by both Hibernate and our JMX Agent
simpleJpaHibernateApp.agents.jmxAgent. -->
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<!-- <property name="hibernate.cache.provider_configuration">/ehcache.cfg.xml</property> -->
<property name="hibernate.cache.use_minimal_puts">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
</session-factory>
同样,我们使用缓存管理器类:
public class AppCacheManager {
private static final Logger log = LoggerFactory.getLogger(AppCacheManager.class);
private static CacheManager manager;
@SuppressWarnings("unused")
private static AppCacheManager INSTANCE = new AppCacheManager();
private AppCacheManager() {
manager = CacheManager.getInstance();
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(manager, mBeanServer, true, true, true, true);
if (log.isInfoEnabled()) {
log.info("Cache Manager was initialized.");
log.info("Cache Regions Detected:");
String[] cacheNames = manager.getCacheNames();
for (String cacheName : cacheNames) {
log.info(" " + cacheName);
}
log.info("Cache disk store path: " + manager.getDiskStorePath());
}
}
我可以用一些cache2k impl替换hibernate配置net.sf.ehcache.hibernate.SingletonEhCacheProvider吗?
以及我应如何基于cache2k采用AppCacheManager?
谢谢!
最佳答案
您可以将cache2k与hibernate结合使用,而无需任何其他代码。
您需要在项目中添加以下依赖项:
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-all</artifactId>
<version>${cache2k-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-jcache</artifactId>
<version>${cache2k-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
</dependency>
休眠配置需要包含:
<hibernate-configuration>
<session-factory>
<property name="cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property>
<property name="hibernate.javax.cache.provider">org.cache2k.jcache.provider.JCacheProvider</property>
<property name="hibernate.javax.cache.uri">hibernate</property>
<!-- .... rest of configuration .... -->
</session-factory>
</hibernate-configuration>
然后,您可以将文件
cache2k-hibernate.xml
添加到类路径,该文件允许进一步配置缓存,例如:<cache2k>
<version>1.0</version>
<!-- if enabled cache2k does not refuse operation in case there is
no configuration for a requested cache name -->
<ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>
<defaults>
<!-- default settings for every cache -->
<cache>
<entryCapacity>100_000</entryCapacity>
</cache>
</defaults>
<caches>
<!-- reduced size for the query cache -->
<cache>
<name>org.hibernate.cache.internal.StandardQueryCache</name>
<entryCapacity>100</entryCapacity>
</cache>
</caches>
</cache2k>