一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色婷婷在线播放 | 公园吃女人奶野战视频 | 福利一区三区 | 美女黄a | 精品欧美一区二区精品久久 | aⅴ天堂小视频 | 四虎精品成人a在线观看 | 99视频免费在线观看 | 国产成人免费在线视频 | 双性太子| 禁止的爱善良的未删减版hd | 精品一区视频 | 四虎最新免费观看网址 | gay台湾无套男同志可播放 | 青青视频国产依人在线 | 忘忧草在线社区WWW日本-韩国 | 鬼畜重口高h合集长短篇 | 学校捏奶揉下面污文h | 无耻之徒第十一季在线观看 | 火影小南被爆羞羞网站进入 | 国产精品福利一区二区亚瑟 | 超级乱淫伦短篇在车上 | 韩国三级在线 | 国产清纯91天堂在线观看 | 免费的伦理片 | 国内精品久久久久久野外 | 国产日日操 | 亚洲免费高清视频 | 国产精品久久久久影院色老大 | 精品在线观看一区 | 色综合网天天综合色中文男男 | 国产精品免费视频一区一 | 把美女屁股眼扒开图片 | 美女在线看永久免费网址 | 日韩高清在线免费看 | 36美女厕所撒尿全过程 | 激情影院费观看 | 99精品国产成人a∨免费看 | 国产一区二区免费视频 | 四虎影院在线免费 | 99视频在线免费观看 |