Согласно документации hibernate необходимо создать 2 других области кэша
- org.hibernate.cache.spi.UpdateTimestampsCache: тиос должен быть настолько большим, насколько это возможно (в идеале никогдасрок действия истекает)
- org.hibernate.cache.internal.StandardQueryCache
Hibernate не может создать их на лету (прежде всего первый)
На самом делеиспользуя ehcahe 3 и hibernate 5, я использую эту конфигурацию:
@Configuration
@EnableCaching
public class CacheConfiguration extends CachingConfigurerSupport
{
@Autowired
private Environment env;
@Bean("cacheManager")
@Override
public org.springframework.cache.CacheManager cacheManager()
{
return new JCacheCacheManager(createCacheManager());
}
private CacheManager createCacheManager()
{
long dimensioneCache = new Long(env.getProperty("arca.context.cache.size"));
long ttlMillisecondi = new Long(env.getProperty("arca.context.cache.ttl"));
org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder.
newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.heap(dimensioneCache)
).withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttlMillisecondi, TimeUnit.MILLISECONDS))).build();
Map<String, org.ehcache.config.CacheConfiguration<?, ?>> caches = createCacheConfigurations(cacheConfiguration);
//Creo la cache di hibernate org.hibernate.cache.spi.UpdateTimestampsCache.
//Dalla documentazione di hibernate https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#caching
ResourcePoolsBuilder rpb = ResourcePoolsBuilder.heap(dimensioneCache*1000000);
org.ehcache.config.CacheConfiguration<Object, Object> eternalCacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class, rpb).withExpiry(Expirations.noExpiration()).build();
caches.put("org.hibernate.cache.spi.UpdateTimestampsCache", eternalCacheConfiguration);
EhcacheCachingProvider provider = getCachingProvider();
DefaultConfiguration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader());
CacheManager result = provider.getCacheManager(provider.getDefaultURI(), configuration);
return result;
}
private Map<String, org.ehcache.config.CacheConfiguration<?, ?>> createCacheConfigurations(org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration)
{
Map<String, org.ehcache.config.CacheConfiguration<?, ?>> caches = new HashMap<>();
// I'm searcing for all objects with @Entity annotation in order to create cache regions
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition bd : scanner.findCandidateComponents("it.test.models"))
{
String className = bd.getBeanClassName();
caches.put(className, cacheConfiguration);
}
//org.hibernate.cache.internal.StandardQueryCache creation
caches.put("org.hibernate.cache.internal.StandardQueryCache", cacheConfiguration);
return caches;
}
private EhcacheCachingProvider getCachingProvider()
{
return (EhcacheCachingProvider) Caching.getCachingProvider();
}
}
В моей конфигурации hibernate я использую эту кэш-память it.olegna.tests.hibernate.cache.config.JCacheRegionFactory
ее код следующий:
public class JCacheRegionFactory extends org.hibernate.cache.jcache.JCacheRegionFactory
{
private static final long serialVersionUID = 1021281213463444167L;
@Override
protected Cache<Object, Object> createCache(String regionName, Properties properties, CacheDataDescription metadata)
{
throw new IllegalArgumentException("Unknown hibernate cache: " + regionName);
}
}