我是新来Hazelcast的。我正在尝试在我的spring boot应用程序中实现缓存。我已经使用两个映射配置(CacheObject和CacheList)为hazlecast创建了一个配置类。我有两个方法objectMethod()和listMethod(),前者返回单个employee对象,后者返回employee对象列表。
我在objectMethod和listMethod上使用了@cacheable注解。问题是只有对象缓存在工作,列表缓存没有在工作。当我在调试模式下运行程序时,对象缓存是返回值,而不是进入方法,但是列表缓存总是执行方法并从数据库中获取值。
我是否遗漏了任何配置或其他内容?
我使用的是Spring boot版本2.1.3.RELEASE、Hazelcast版本3.11.1和Hazelcast-spring版本3.11.1。
我尝试了spring actuator缓存url来查看缓存,但我只看到了CacheObject,而没有看到CacheList。
http://localhost:8080/actuator/caches
{"cacheManagers":{"cacheManager":{"caches":{"CacheObject":{"target":"com.hazelcast.map.impl.proxy.MapProxyImpl"}}}}}配置类
@Configuration
public class HazelcastCacheConfig {
@Bean
public Config cacheConfig() {
return new Config().setInstanceName("hazelcast-instance")
.addMapConfig(new MapConfig().setName("CacheObject")
.setMaxSizeConfig(new MaxSizeConfig(100, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
.setEvictionPolicy(EvictionPolicy.LRU).setTimeToLiveSeconds(86400))
.addMapConfig(new MapConfig().setName("CacheList")
.setMaxSizeConfig(new MaxSizeConfig(100, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
.setEvictionPolicy(EvictionPolicy.LRU).setTimeToLiveSeconds(86400));
}@cacheable注解
@Cacheable(value="CacheList")
public List<Employee> getEmployeeList(String a, String b, String b){
//Query
return employeeList;
}@Cacheable(value="CacheObject")
public Employee getEmployeeObject(String a, String b, String v) {
//Query
return employeeObject;
}Employee类
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
private string a,
private string b,
private string c,
private UUID d,
private Map<String,String> e;
}发布于 2019-05-27 23:59:27
您是否以相同的方式使用这两个缓存?如果你不使用代理对象(不要使用依赖注入),Cachable就不能工作。
另外,您是如何配置缓存管理器的?
package test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import com.hazelcast.test.TestHazelcastInstanceFactory;
@RunWith(SpringRunner.class)
@ContextConfiguration(
classes= { CacheableTest.CacheConfig.class, CacheableTest.Repo.class }
)
@EnableCaching
public class CacheableTest
{
@Configuration
public static class CacheConfig
{
@Bean
public HazelcastInstance hzInstance() {
return Hazelcast.newHazelcastInstance();
}
@Bean
public CacheManager cacheManager(HazelcastInstance hzInstance) {
return new HazelcastCacheManager(hzInstance);
}
}
@Repository
public static class Repo {
public static int callCountA;
public static int callCountB;
@Cacheable("a")
public String getA(String key) {
++callCountA;
return key;
}
@Cacheable("b")
public String getB(String key) {
++callCountB;
return key;
}
}
@Autowired
public Repo repo;
@Test
public void test() {
String key = "a";
assertEquals(0, Repo.callCountA);
System.out.println(repo.getA(key));
System.out.println(repo.getA(key));
assertEquals(1, Repo.callCountA);
assertEquals(0, Repo.callCountB);
System.out.println(repo.getB(key));
System.out.println(repo.getB(key));
assertEquals(1, Repo.callCountB);
}
}https://stackoverflow.com/questions/56327380
复制相似问题