• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot hazelcast缓存中间件的实例代码

springboot 搞代码 4年前 (2022-01-05) 26次浏览 已收录 0个评论

这篇文章主要介绍了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 fetchAllUsers(); List 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 fetchAllUsers(){ List 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 fetchAllUsers(String name) { List 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

运行程序

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

 @ActivePro<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>files("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缓存中间件的实例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot hazelcast缓存中间件的实例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址