我正在成功地使用Hazelcast作为Hibernate的L2分布式缓存。看起来,查询缓存没有得到分发。“hibernate查询缓存”不可能使用分布式缓存吗?还是我缺少了确保“查询缓存”也被分发的配置?使用Hazelcast 3.2.6和Grails 2.2.3。
发布于 2015-01-21 18:00:43
“注意: QueryCache总是本地节点,从未在Hazelcast集群中分发”它来自hazelcast文档。如果您在hibernate上使用HazelcastLocalCacheRegionFactory,regionFactory配置会将数据存储在本地节点中,并在本地更新/删除条目时发送无效消息。
这是hazelcast文档http://docs.hazelcast.org/docs/3.4/manual/pdf/hazelcast-documentation-3.4.pdf页面177
发布于 2015-12-01 13:44:36
正如@Luís Otávio Braga所建议的,我实现了使用hazelcast分发hibernate查询缓存的解决方案。完成答案有几个不同之处和更多信息:
所以完整的代码是:
AbstractHazelcastCacheRegionFactory和hibernate一样,只有none--最终的buildQueryResultsRegion()。
DistributeQueryCacheHazelcastFactory:
/**
* Similar to com.hazelcast.hibernate.HazelcastCacheRegionFactory, only differences:
* 1. Extends our own implementation of AbstractHazelcastCacheRegionFactory.
* 2. Override buildQueryResultsRegion() to return DistributedQueryCacheDataRegion (that's why we needed our own
* implementation of AbstractHazelcastCacheRegionFactory to be able to override).
*
*/
public class DistributeQueryCacheHazelcastFactory extends AbstractHazelcastCacheRegionFactory {
private static final long serialVersionUID = 1L;
public DistributeQueryCacheHazelcastFactory() {
super();
}
public DistributeQueryCacheHazelcastFactory(final HazelcastInstance instance) {
super(instance);
}
public DistributeQueryCacheHazelcastFactory(final Properties properties) {
super(properties);
}
@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
return new DistributedQueryCacheDataRegion(instance, regionName, properties, new IMapRegionCache(regionName,
instance, properties, null));
}
@Override
public CollectionRegion buildCollectionRegion(final String regionName, final Properties properties,
final CacheDataDescription metadata) throws CacheException {
return new HazelcastCollectionRegion<IMapRegionCache>(instance, regionName, properties, metadata,
new IMapRegionCache(regionName, instance, properties, metadata));
}
@Override
public EntityRegion buildEntityRegion(final String regionName, final Properties properties,
final CacheDataDescription metadata) throws CacheException {
return new HazelcastEntityRegion<IMapRegionCache>(instance, regionName, properties, metadata,
new IMapRegionCache(regionName, instance, properties, metadata));
}
@Override
public TimestampsRegion buildTimestampsRegion(final String regionName, final Properties properties)
throws CacheException {
return new HazelcastTimestampsRegion<IMapRegionCache>(instance, regionName, properties, new IMapRegionCache(
regionName, instance, properties, null));
}
}
DistributedQueryCacheDataRegion:
public class DistributedQueryCacheDataRegion extends AbstractGeneralRegion<IMapRegionCache> implements
QueryResultsRegion {
protected DistributedQueryCacheDataRegion(HazelcastInstance instance, String name, Properties props,
IMapRegionCache cache) {
super(instance, name, props, cache);
}
}
最后,需要将hibernate配置hibernate.cache.region.factory_class中的工厂更改为新的DistributeQueryCacheHazelcastFactory类。
https://stackoverflow.com/questions/27601596
复制相似问题