如何在Hazelcast缓存映射上设置TTL(可春季缓存)



我使用的是带有弹簧引导的Hazelcast集群缓存。我使用的是4.2版本的榛子铸件。

缓存工作正常,但不会在TTL之后将数据从缓存映射中逐出。始终保持相同的数据。我尝试了很多设置ttl的方法,但都没有成功。

这是我的机会配置类

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfig {
@Value("${hazelcast.cluster-name}")
private String hzClusterName;
@Value("${hazelcast.address}")
private String hzAddress;
@Bean
HazelcastInstance hazelcastInstance() {
return HazelcastClient.newHazelcastClient(clientConfig());
}
@Bean
public ClientConfig clientConfig() {
ClientConfig cfg = ClientConfig.load();
cfg.setClusterName(hzClusterName);
cfg.getNetworkConfig().addAddress(hzAddress);
return cfg;
}
@Bean
public CacheManager cacheManager() {
return new HazelcastCacheManager(hazelcastInstance());
}
}

我使用可缓存的缓存类

@Cacheable("items")
public String getInfo(String systemSkuRef) {
logger.debug("Not using cache, fetch and put in cache");
return "Item Info";
}

我尝试通过设置在src/main/resources中使用hazelcast.yaml文件

hazelcast:
network:
public-address: 
cluster-name: dev
map:
items:
time-to-live-seconds: 120
max-idle-seconds: 60
eviction:
eviction-policy: LRU
max-size-policy: PER_NODE
size: 1000

有其他方法吗?或者我怎么能做到这一点,谢谢你的帮助

有两种拓扑可以使用Hazelcast:嵌入式和客户端服务器。Java Spring配置配置Hazelcast客户端,但是hazelcast.yaml专用于嵌入式模式。

尝试在Hazelcast服务器中使用hazelcast.yaml配置。或者在Hazelcast客户端中配置缓存,例如,如下所示:

@Bean
HazelcastInstance hazelcastInstance() {
HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig());
instance.getConfig().addMapConfig(new MapConfig("items").setTimeToLiveSeconds(120));
return instance;
}

最新更新