springboot~hazelcast缓存中间件

本文介绍如何在Spring Boot应用中使用Hazelcast分布式缓存。通过配置依赖、设置缓存策略并结合具体业务场景,展示了Hazelcast的高效利用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

缓存来了

在dotnet平台有自己的缓存框架,在java springboot里当然了集成了很多,而且缓存的中间件也可以进行多种选择,向redis, hazelcast都是分布式的缓存中间件,今天主要说一下后者的实现。

添加依赖包
dependencies {
    compile("org.springframework.boot:spring-boot-starter-cache")
    compile("com.hazelcast:hazelcast:3.7.4")
    compile("com.hazelcast:hazelcast-spring:3.7.4")
}
bootRun {
    systemProperty "spring.profiles.active", "hazelcast-cache"
}
config统一配置
@Configuration
@Profile("hazelcast-cache")//运行环境名称
public class HazelcastCacheConfig {

  @Bean
  public Config hazelCastConfig() {

    Config config = new Config();
    config.setInstanceName("hazelcast-cache");

    MapConfig allUsersCache = new MapConfig();
    allUsersCache.setTimeToLiveSeconds(3600);
    allUsersCache.setEvictionPolicy(EvictionPolicy.LFU);
    config.getMapConfigs().put("alluserscache", allUsersCache);

    MapConfig usercache = new MapConfig();
    usercache.setTimeToLiveSeconds(3600);//超时时间为1小时
    usercache.setEvictionPolicy(EvictionPolicy.LFU);
    config.getMapConfigs().put("usercache", usercache);//usercache为缓存的cachename

    return config;
  }

}
添加仓储
public interface UserRepository {

  List<UserInfo> fetchAllUsers();

  List<UserInfo> fetchAllUsers(String name);
}


@Repository
@Profile("hazelcast-cache")// 指定在这个hazelcast-cache环境下,UserRepository的实例才是UserInfoRepositoryHazelcast
public class UserInfoRepositoryHazelcast implements UserRepository {

  @Override
  @Cacheable(cacheNames = "usercache", key = "#root.methodName")// 无参的方法,方法名作为key
  public List<UserInfo> fetchAllUsers(){
    List<UserInfo> list = new ArrayList<>();
    list.add(UserInfo.builder().phone("135").userName("zzl1").createAt(LocalDateTime.now()).build());
    list.add(UserInfo.builder().phone("136").userName("zzl2").createAt(LocalDateTime.now()).build());
    return list;
  }
  @Override
  @Cacheable(cacheNames = "usercache", key = "{#name}") // 方法名和参数组合做为key
  public List<UserInfo> fetchAllUsers(String name) {
    List<UserInfo> list = new ArrayList<>();
    list.add(UserInfo.builder().phone("135").userName("zzl1").createAt(LocalDateTime.now()).build());
    list.add(UserInfo.builder().phone("136").userName("zzl2").createAt(LocalDateTime.now()).build());
    return list;
  }
}
配置profile

application.yml开启这个缓存的环境

  profiles.active: hazelcast-cache
运行程序

可以在单元测试里进行测试,调用多次,方法体只进入一次,这就是缓存成功了。

@ActiveProfiles("hazelcast-cache")
public class UserControllerTest extends BaseControllerTest {
  @Test
  public void fetchUsers() {
    getOk();
    //test caching
    getOk();
  }

  private WebTestClient.ResponseSpec getOk() {
    return http.get()
        .uri("/users/all/zzl")
        .exchange()
        .expectStatus().isOk();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值