我正在尝试用Hazelcast配置Spring CacheManager。另外,我想配置Hazelcast的Near Cache,这样我就可以检索我的缓存对象的(已经反序列化的)实例。
这是我的配置
@Bean
public HazelcastInstance hazelcastConfig() {
val config = new Config().setInstanceName("instance");
val serializationConfig = config.getSerializationConfig();
addCacheConfig(config, "USERS")
serializationConfig.addSerializerConfig(new SerializerConfig()
.setImplementation(getSerializer())
.setTypeClass(User.class)
return Hazelcast.newHazelcastInstance(config);
}
@Bean
public CacheManager cacheManager(HazelcastInstance hazelcastInstance) {
return new HazelcastCacheManager(hazelcastInstance);
}
@Bean
public PlatformTransactionManager chainedTransactionManager(PlatformTransactionManager jpaTransactionManager, HazelcastInstance hazelcastInstance) {
return new ChainedTransactionManager(
jpaTransactionManager,
new HazelcastTransactionManager(hazelcastInstance)
);
}
// Configure Near Cache
private void addCacheConfig(Config config, String cacheName) {
val nearCacheConfig = new NearCacheConfig()
.setInMemoryFormat(OBJECT)
.setCacheLocalEntries(true)
.setInvalidateOnChange(false)
.setTimeToLiveSeconds(hazelcastProperties.getTimeToLiveSeconds())
.setEvictionConfig(new EvictionConfig()
.setMaxSizePolicy(ENTRY_COUNT)
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(hazelcastProperties.getMaxEntriesSize()));
config.getMapConfig(cacheName)
.setInMemoryFormat(BINARY)
.setNearCacheConfig(nearCacheConfig);
}从缓存中保存和检索都可以正常工作,但我的对象在每次缓存命中时都会被反序列化。我想使用NearCache来避免这种反序列化时间,但它不起作用。我还尝试了二进制内存格式。
Hazelcast能做到这一点吗?或者,即使我有一个NearCache,这个反序列化也总是被执行吗?
谢谢
发布于 2020-03-07 04:59:39
因此,经过几次更改后,它现在可以工作了。以下是我的结论:
因此,为了让NearCache与Spring Cache一起工作,所有缓存的对象都应该是Immutable。这意味着最终的类和最终的字段。而且,它们都扩展了Serializable接口。
https://stackoverflow.com/questions/60310584
复制相似问题