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 hazelcast缓存中间件的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
- Spring Boot Web 静态文件缓存处理的方法
- spring boot+spring cache实现两级缓存(redis+caffeine)
- Spring Boot 中使用cache缓存的方法
- SpringBoot项目中使用redis缓存的方法步骤
- 实例详解Spring Boot实战之Redis缓存登录验证码
- Spring Boot 基于注解的 Redis 缓存使用详解
- springboot集成spring cache缓存示例代码
- springboot+EHcache 实现文章浏览量的缓存和超时更新
- 详解Spring Boot使用redis实现数据缓存
- Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)
相关文章
基于Java实现杨辉三角 LeetCode Pascal''s Triangle
这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下2016-01-01完美解决IDEA Ctrl+Shift+f快捷键突然无效的问题
这篇文章主要介绍了完美解决IDEA Ctrl+Shift+f快捷键突然无效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-02-02排查Failed to validate connection com.mysql.cj.jdbc.Connec
这篇文章主要介绍了Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl问题排查,具有很好的参考价值,希望对大家有所帮助2023-02-02
最新评论