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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产日韩成人综合天堂 | 99爱在线观看精品视频 | 亚洲高清影院 | 欧美成人免费观看的 | 国产自产在线 | 1717国产精品视频免费 | 91视频a| 免费观看无人区完整版 | 午夜影院c绿象 | 天堂8在线天堂资源在线 | 精品蜜臀AV在线天堂 | 香蕉免费高清完整 | 欧美国产精品 | 日韩精品免费一区二区三区 | 变态人shou交小说 | 黑人又大又硬又粗再深一点 | 国产激情久久久久影院小草 | 亚洲成人aa| 日本高清二三四本2021 | 人人人人看人人人做人人 | 扒开大腿狠狠挺进视频 | 精品一区二区三区在线视频观看 | 国产一成人精品福利网站 | 挺进白嫩老师下面视频 | 人人最怕九月羊 | 欧美成人福利视频 | 日产精品一二三四区国产 | 美女的隐私无遮挡的网页 | 图片专区小说专区卡通动漫 | java hd国产高清 | 日韩国产成人 | jizz 日本亚洲 | 欧美午夜性春猛交bbb | 成年人网站免费在线观看 | 婷婷综合久久 | 操老妇| 亚洲视频一区网站 | 暖暖免费高清完整版观看日本 | 久久久久久免费高清电影 | 成人小视频在线观看 | 国产精品久久久久久久久免费 |