激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品亚洲一区二区三区久久 | 久久久久av电影 | 91一级毛片 | 国产高潮失禁喷水爽到抽搐视频 | 一级毛片在线免费观看 | 久久久中精品2020中文 | 精品一区二区在线播放 | 黄色网络免费看 | 日韩黄在线观看 | 被啪羞羞视频在线观看 | 久久久久久久久久综合 | 免费a级片在线观看 | 婷婷亚洲一区二区三区 | 一区二区免费 | 日韩大片在线永久观看视频网站免费 | 中国性xxx| 超久久| 久久久久久久久久亚洲精品 | 国产精品亚洲yourport | 久久草在线观看视频 | 欧美日韩在线中文字幕 | 女人叉开腿让男人桶 | 免费一级片观看 | 欧美成人h版在线观看 | 性感美女一级毛片 | 中文字幕在线观看www | 免费观看一区二区三区 | 久久久久成人免费 | 久久久一区二区三区精品 | 日韩精品中文字幕一区二区三区 | 98国内自拍在线视频 | 午夜网站视频 | 国产成人精品一区二区仙踪林 | 国产自在线 | 国产精品久久久久久久娇妻 | 国产精品啪一品二区三区粉嫩 | 久久久久久久99 | 美女被免费网站在线软件 | 九九精品久久 | 免费放黄网站在线播放 | 嫩呦国产一区二区三区av |