【问题标题】:How to configure multiple cache in spring boot using hazelcast?如何使用hazelcast在spring boot中配置多个缓存?
【发布时间】:2019-10-13 02:34:06
【问题描述】:

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

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

员工类

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

【问题讨论】:

    标签: spring-boot caching hazelcast


    【解决方案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);
    
      }
    }
    

    【讨论】:

    • 我从同一个类中调用了员工列表方法,这就是问题所在。在您回答之后,我通过使用自动装配对象从另一个类调用该方法来尝试它并且它有效。我还可以在执行器缓存 url 中看到它们。我没有配置我的缓存管理器。我需要配置它们吗?我刚刚关注了这个博客onlinetutorialspoint.com/spring-boot/… 有没有在线资源可以让我了解更多关于spring boot hazlecast 缓存的信息?感谢您的帮助:)
    • 如果一切正常,那么您不必明确创建自己的(显然 Hazelcast Spring 为您创建了一个)。至于材料,我只能推荐 Hazelcast docs 和 Spring docs。不幸的是,我手头没有任何其他好的材料。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多