可以在hazelcast中使用“ cacheResolver”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了可以在hazelcast中使用“ cacheResolver”相关的知识,希望对你有一定的参考价值。

我正在使用spring缓存和hazelcast,我只是想将响应保存到控制器中:

@Bean
@Override
public CacheManager cacheManager() 
    return new HazelcastCacheManager(hazelcastInstance);


//creating hazelcast cache config.
@Bean
public HazelcastClientCacheManager hazelcastClientCacheManager() 
    CachingProvider provider = 
    HazelcastClientCachingProvider.createCachingProvider(hazelcastInstance);
    HazelcastClientCacheManager cacheManager = (HazelcastClientCacheManager) provider.getCacheManager();
    cacheManager.getHazelcastInstance().getConfig().getSerializationConfig().getByteOrder();

    Duration oneHours = Duration.ONE_MINUTE;
    MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>();
    configuration.setTypes(Object.class, Object.class)
        .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(oneHours))
        .setStatisticsEnabled(true);
    cacheManager.createCache("mycache", configuration);*

    return cacheManager;


//My CacheResolver 
@Bean
@Override
public CacheResolver cacheResolver() 
    return new CustomCacheResolver(cacheManager());

有问题,在我的CacheResolver.class中:

public class CustomCacheResolver implements CacheResolver 
    private final CacheManager cacheManager;

    @Override
    public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) 
        Collection<Cache> caches = new ArrayList<>();
        caches.add(cacheManager.getCache("mycache"));
        return caches;
    

发件人:

@Cacheable(cacheResolver = "cacheResolver", key = "#uuid+", 
    unless="#result.size() == 0")
myMethod(String uuid)

问题:CustomCacheResolver中的resolveCaches必须返回缓存集合,但是我在hazelcast中创建的缓存是ICache。

有什么想法吗?我需要使用该缓存及其配置,ttl等。预先谢谢。!

Java 11。春季靴弹簧缓存。hazelcast。

答案
解决方案正在使用:导入com.hazelcast.spring.cache.HazelcastCacheManager

类似这样的东西:

@Bean @Override public CacheManager cacheManager() return new HazelcastCacheManager(hazelcastInstance); @Bean @Override public CacheResolver cacheResolver() return new CustomCacheResolver(concurrentMapCacheManager()); @Bean public CacheManager concurrentMapCacheManager() return new ConcurrentMapCacheManager() protected Cache createConcurrentMapCache(final String name) return new ConcurrentMapCache(name, CacheBuilder.newBuilder().expireAfterWrite(ttl, TimeUnit.SECONDS).build().asMap(), false); ;

并创建CustomCacheResolver.class扩展了CacheResolver,就足够了。

以上是关于可以在hazelcast中使用“ cacheResolver”的主要内容,如果未能解决你的问题,请参考以下文章

Hazelcast 正在使用大量 JVM 线程

Hazelcast如何进行分区

了解 Hazelcast 中的这些指标名称的含义

Hazelcast集群原理分析

Hazelcast介绍与使用

Hazelcast集群服务——Hazelcast基本配置