如何使用hazelcast在Spring Boot中配置多个缓存?

时间:2019-05-27 13:50:22

标签: spring-boot caching hazelcast

我是Hazelcast的新手。我正在尝试在Spring Boot应用程序中实现缓存。我创建了带有两个映射配置(CacheObject和CacheList)的hazlecast配置类。我有两个方法objectMethod()返回一个雇员对象,而listMethod()返回一个雇员对象列表。

我在objectMethod和listMethod上使用@cacheable批注。问题是只有对象高速缓存正在工作,列表高速缓存没有工作。当我在调试模式下运行程序时,对象缓存不返回方法就返回值,但列表缓存始终执行方法并从数据库获取值。

我是否缺少任何配置或其他内容?

我使用的是Spring Boot版本2.1.3RELEASE,Hazelcast版本3.11.1和Hazelcast-spring版本3.11.1。

我尝试了弹簧执行器缓存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(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;
    }

员工阶层

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;
}

1 个答案:

答案 0 :(得分:1)

您是否以相同的方式使用两个缓存?如果您不使用代理对象(请不要使用依赖项注入),则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);

  }
}
相关问题