Dev*_*per 5 java hazelcast spring-boot spring-cache
我正在使用带有 Spring Boot 的 Hazelcast 集群缓存。我使用的是 4.2 版本的 hazelcast。
缓存工作正常,但在 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());
}
}
Run Code Online (Sandbox Code Playgroud)
我使用可缓存的缓存类
@Cacheable("items")
public String getInfo(String systemSkuRef) {
logger.debug("Not using cache, fetch and put in cache");
return "Item Info";
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过设置在 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
Run Code Online (Sandbox Code Playgroud)
有没有其他方法或者我怎样才能实现这一目标,谢谢您的帮助
您可以在两种拓扑中使用 Hazelcast:嵌入式和客户端-服务器。您的 Java Spring 配置会配置 Hazelcast 客户端,但您的配置hazelcast.yaml专用于嵌入式模式。
尝试hazelcast.yaml在 Hazelcast 服务器中使用您的配置。或者在 Hazelcast 客户端中配置缓存,例如,如下所示:
@Bean
HazelcastInstance hazelcastInstance() {
HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig());
instance.getConfig().addMapConfig(new MapConfig("items").setTimeToLiveSeconds(120));
return instance;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4891 次 |
| 最近记录: |