使用 Spring 缓存和 Hazelcast 近缓存
Using Spring Cache with Hazelcast Near Cache
我正在尝试使用 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,这个反序列化总是执行吗?
谢谢
因此,经过一些更改后,它现在可以正常工作了。这是我的结论:
因此,为了让 NearCache 与 Spring 缓存一起工作,您所有的缓存对象都应该是 Immutable。这意味着 final 类 和 final 字段。此外,它们都扩展了 Serializable 接口。
我正在尝试使用 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,这个反序列化总是执行吗?
谢谢
因此,经过一些更改后,它现在可以正常工作了。这是我的结论:
因此,为了让 NearCache 与 Spring 缓存一起工作,您所有的缓存对象都应该是 Immutable。这意味着 final 类 和 final 字段。此外,它们都扩展了 Serializable 接口。