springboot~hazelcast缓存中间件
缓存来了
在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();
}
}
合集:
springboot(1)
, DotNetCore
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2017-08-22 DotNetCore跨平台~EFCore数据上下文的创建方式
2017-08-22 DotNetCore跨平台~EFCore2.0连接Mysql的烦恼-已解决
2016-08-22 WebApi系列~FromUri参数自动解析成实体的要求
2016-08-22 Redis学习笔记~conf自主集群模式
2011-08-22 工厂方法模式在开发中的应用