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

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot Hazelcast Caching 使用和配置詳解

Spring Boot Hazelcast Caching 使用和配置詳解

2021-05-30 15:26zhongzunfa Java教程

這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文將展示spring boot 結合 hazelcast 的緩存使用案例。

1. project structure

Spring Boot Hazelcast Caching 使用和配置詳解

2. maven dependencies

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.zzf</groupid>
  <artifactid>spring-boot-hazelcast</artifactid>
  <version>1.0-snapshot</version>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.1.release</version>
  </parent>
 
  <dependencies>
 
    <!-- spring boot  -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-cache</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-actuator</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <!-- hazelcast jar -->
    <dependency>
      <groupid>com.hazelcast</groupid>
      <artifactid>hazelcast-all</artifactid>
      <version>3.10.1</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

3. hazelcast caching service

通過使用

  • @cachable注釋來注釋play方法,將緩存后續調用的結果。
  • @cacheevict(allentries=true)清除緩存中的所有條目。
  • @cacheconfig(cachenames=”instruments”)注冊了帶有指定緩存的spring框架緩存注釋的所有方法。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@service
@cacheconfig(cachenames = "instruments")
public class musicservice {
 
  private static logger log = loggerfactory.getlogger(musicservice.class);
  @cacheevict(allentries = true)
  public void clearcache(){}
 
  // 表示的是屬性為 trombone 就進行緩存
  @cacheable(condition = "#instrument.equals('trombone')")
  public string play(string instrument){
 
    log.info("executing: " + this.getclass().getsimplename() + ".play(\"" + instrument + "\");");
    return "playing " + instrument + "!";
  }
}

4. hazelcast caching configuration

如果類路徑下存在hazelcast, spring boot 將會自動創建hazelcast 的實例。

下面將介紹兩種加載的方式:

  • 使用java 配置的方式
  • 使用hazelcast.xml xml 的配置

4.1 hazelcast java configuration

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@configuration
public class hazelcastconfiguration {
  @bean
  public config hazelcastconfig(){
    return new config().setinstancename("hazelcast-instance")
        .addmapconfig(
              new mapconfig()
                  .setname("instruments")
                  .setmaxsizeconfig(new maxsizeconfig(200, maxsizeconfig.maxsizepolicy.free_heap_size))
                  .setevictionpolicy(evictionpolicy.lru)
                  .settimetoliveseconds(20)
        );
  }
}

4.2 hazelcast xml configuration

可以使用xml 配置 hazelcast , 在src/main/resources 添加一個文件hazelcast.xml

spring boot 將會自動注入配置文件, 當然也可以指定路徑路徑, 使用屬性spring.hazelcast.config 配置在yml 或者properties 文件中, 例如下面所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<hazelcast
    xsi:schemalocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config.xsd"
    xmlns="http://www.hazelcast.com/schema/config"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
 
  <map name="instruments">
    <max-size>200</max-size>
    <eviction-policy>lfu</eviction-policy>
    <time-to-live-seconds>20</time-to-live-seconds>
  </map>
</hazelcast>

具體的application.properties 和 application.yml 文件顯示

?
1
2
3
4
# application.yml
spring:
 hazelcast:
  config: classpath:config/hazelcast.xml
?
1
2
# application.properties
spring.hazelcast.config=classpath:config/hazelcast.xml

5. 訪問controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@controller
public class hazelcastcontroller {
 
  private logger logger = loggerfactory.getlogger(hazelcastcontroller.class);
 
  @autowired
  private musicservice musicservice;
 
  @autowired
  private cachemanager cachemanager;
 
  @requestmapping("/hezelcast")
  @responsebody
  public void gethazelcast(){
 
    logger.info("spring boot hazelcast caching example configuration");
    logger.info("using cache manager: " + cachemanager.getclass().getname());
 
    // 清空緩存
    musicservice.clearcache();
 
    // 調用方法
    play("trombone");
    play("guitar");
 
    play("trombone");
    play("guitar");
 
    play("bass");
    play("trombone");
  }
 
  private void play(string instrument){
    logger.info("calling: " + musicservice.class.getsimplename() + ".play(\"" + instrument + "\");");
    musicservice.play(instrument);
  }
}

6. bootstrap hazelcast caching application

使用注解@enablecaching 開啟緩存機制.

?
1
2
3
4
5
6
7
8
9
10
@enablecaching
@springbootapplication
public class hazelcastapplication{
 
  private logger logger = loggerfactory.getlogger(hazelcastapplication.class);
 
  public static void main(string[] args) {
    springapplication.run(hazelcastapplication.class, args);
  }
}

7. 訪問和解釋

2018-06-02 22:15:18.488  info 41728 --- [nio-8080-exec-4] c.h.i.p.impl.partitionstatemanager       : [192.168.1.1]:5701 [dev] [3.10.1] initializing cluster partition table arrangement...
2018-06-02 22:15:18.521  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("trombone");
2018-06-02 22:15:18.558  info 41728 --- [nio-8080-exec-4] c.z.s.hazelcast.service.musicservice     : executing: musicservice.play("trombone");
2018-06-02 22:15:18.563  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("guitar");
2018-06-02 22:15:18.563  info 41728 --- [nio-8080-exec-4] c.z.s.hazelcast.service.musicservice     : executing: musicservice.play("guitar");
2018-06-02 22:15:18.563  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("trombone");
2018-06-02 22:15:18.564  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("guitar");
2018-06-02 22:15:18.565  info 41728 --- [nio-8080-exec-4] c.z.s.hazelcast.service.musicservice     : executing: musicservice.play("guitar");
2018-06-02 22:15:18.565  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("bass");
2018-06-02 22:15:18.565  info 41728 --- [nio-8080-exec-4] c.z.s.hazelcast.service.musicservice     : executing: musicservice.play("bass");
2018-06-02 22:15:18.566  info 41728 --- [nio-8080-exec-4] c.z.s.h.controller.hazelcastcontroller   : calling: musicservice.play("trombone");

從上面的可以看到 只有trombone , 才會直接訪問緩存信息, 正是在musicservice 類中的方法play 上時候注解進行過濾:
@cacheable(condition = “#instrument.equals(‘trombone')”)

本文參考地址: https://memorynotfound.com/spring-boot-hazelcast-caching-example-configuration/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/zhongzunfa/article/details/80551753

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产午夜视频在线观看网站 | 特级老女人淫片高清视频 | chinese国产人妖videos | 国产caoni555在线观看 | 欧美成a人片免费看久久 | 欧美亚洲高清日韩成人 | 国产91精品区| 欧美国产日韩在线播放 | 放荡警察巨r麻麻出轨小说 范冰冰特黄xx大片 饭冈加奈子在线播放观看 法国老妇性xx在线播放 | 免费看60分钟大片视频播放 | 国产裸舞在线一区二区 | 国产播放啪视频免费视频 | 日本性爱 | 国产1区精品 | 啊皇上你好大要知画 | 国产99在线a视频 | 2015小明台湾永久区域免费 | 久久精品国产免费播放 | 师尊被各种play打屁股 | 日本暖暖在线视频 | 天堂网在线.www天堂在线视频 | 色综合中文字幕天天在线 | 日本精品久久久久久久久免费 | 国产日韩精品一区二区在线观看 | 国产午夜精品不卡视频 | 成年人网站免费在线观看 | 免费一区二区 | 调教女警花穿环上班 | 四虎免费影院4hu永久免费 | 日本tube24xxxxx| 国产精品资源在线观看网站 | 日本三级在丈面前被耍了 | 天堂精品高清1区2区3区 | 大逼美女| 午夜一区二区福利视频在线 | 506rr亚洲欧美 | 国产人va在线 | bl动漫在线观看 | 四虎在线精品观看免费 | 99热视| 亚欧有色在线观看免费版高清 |