首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用hazelcast在spring boot中配置多个缓存?

如何使用hazelcast在spring boot中配置多个缓存?
EN

Stack Overflow用户
提问于 2019-05-27 21:50:22
回答 1查看 2.1K关注 0票数 0

我是新来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。

代码语言:javascript
运行
复制
http://localhost:8080/actuator/caches

{"cacheManagers":{"cacheManager":{"caches":{"CacheObject":{"target":"com.hazelcast.map.impl.proxy.MapProxyImpl"}}}}}

配置类

代码语言:javascript
运行
复制
@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注解

代码语言:javascript
运行
复制
@Cacheable(value="CacheList")
    public List<Employee> getEmployeeList(String a, String b, String b){
        //Query
        return employeeList;

    }
代码语言:javascript
运行
复制
@Cacheable(value="CacheObject")
    public Employee getEmployeeObject(String a, String b, String v) {

        //Query
         return employeeObject;
    }

Employee类

代码语言:javascript
运行
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-27 23:59:27

您是否以相同的方式使用这两个缓存?如果你不使用代理对象(不要使用依赖注入),Cachable就不能工作。

另外,您是如何配置缓存管理器的?

代码语言:javascript
运行
复制
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);

  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56327380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档