如何使用 hazelcast 在 spring 引导中配置多个缓存?
How to configure multiple cache in spring boot using hazelcast?
我是 Hazelcast 的新手。我正在尝试在我的 spring 引导应用程序中实施缓存。我已经使用两个地图配置(CacheObject 和 CacheList)为 hazlecast 创建了一个配置 class。我有两个方法 objectMethod() returns 单个员工对象和 listMethod() returns 员工对象列表。
我在 objectMethod 和 listMethod 上使用了@cacheable 注释。问题是只有对象缓存在工作,列表缓存不工作。当我 运行 程序处于调试模式时,对象缓存是 returns 值而不进入方法,但列表缓存总是执行方法并从数据库中获取值。
我是否缺少任何配置或其他任何东西?
我正在使用 Spring 引导版本 2.1.3.RELEASE、Hazelcast 版本 3.11.1 和 Hazelcast-spring 版本 3.11.1.
我尝试 spring 执行器缓存 url 查看缓存,但我只看到 CacheObject 而不是 CacheList。
http://localhost:8080/actuator/caches
{"cacheManagers":{"cacheManager":{"caches":{"CacheObject":{"target":"com.hazelcast.map.impl.proxy.MapProxyImpl"}}}}}
配置class
@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;
}
员工class
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;
}
您是否以相同的方式使用两个缓存?如果您不使用代理对象(不要使用依赖注入),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);
}
}
我是 Hazelcast 的新手。我正在尝试在我的 spring 引导应用程序中实施缓存。我已经使用两个地图配置(CacheObject 和 CacheList)为 hazlecast 创建了一个配置 class。我有两个方法 objectMethod() returns 单个员工对象和 listMethod() returns 员工对象列表。
我在 objectMethod 和 listMethod 上使用了@cacheable 注释。问题是只有对象缓存在工作,列表缓存不工作。当我 运行 程序处于调试模式时,对象缓存是 returns 值而不进入方法,但列表缓存总是执行方法并从数据库中获取值。
我是否缺少任何配置或其他任何东西?
我正在使用 Spring 引导版本 2.1.3.RELEASE、Hazelcast 版本 3.11.1 和 Hazelcast-spring 版本 3.11.1.
我尝试 spring 执行器缓存 url 查看缓存,但我只看到 CacheObject 而不是 CacheList。
http://localhost:8080/actuator/caches
{"cacheManagers":{"cacheManager":{"caches":{"CacheObject":{"target":"com.hazelcast.map.impl.proxy.MapProxyImpl"}}}}}
配置class
@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;
}
员工class
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;
}
您是否以相同的方式使用两个缓存?如果您不使用代理对象(不要使用依赖注入),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);
}
}