服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot hazelcast缓存中间件的实例代码

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

2021-05-27 13:47张占岭 Java教程

这篇文章主要介绍了springboot hazelcast缓存中间件的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

缓存来了

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

添加依赖包

?
1
2
3
4
5
6
7
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统一配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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;
 }
}

添加仓储

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

运行程序

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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缓存中间件的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/lori/p/9518892.html

延伸 · 阅读

精彩推荐