最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot Hazelcast Caching 使用和配置詳解

 更新時(shí)間:2018年09月07日 08:59:58   作者:zhongzunfa  
這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文將展示spring boot 結(jié)合 Hazelcast 的緩存使用案例。

1. Project Structure

2. Maven Dependencies

<?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方法,將緩存后續(xù)調(diào)用的結(jié)果。
  • @CacheEvict(allEntries=true)清除緩存中的所有條目。
  • @CacheConfig(cachenames=”instruments”)注冊(cè)了帶有指定緩存的spring框架緩存注釋的所有方法。
@Service
@CacheConfig(cacheNames = "instruments")
public class MusicService {

  private static Logger log = LoggerFactory.getLogger(MusicService.class);
  @CacheEvict(allEntries = true)
  public void clearCache(){}

  // 表示的是屬性為 trombone 就進(jìn)行緩存
  @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 將會(huì)自動(dòng)創(chuàng)建Hazelcast 的實(shí)例。

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

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

4.1 Hazelcast Java Configuration

@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 添加一個(gè)文件hazelcast.xml

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

<?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 文件顯示

# application.yml
spring:
 hazelcast:
  config: classpath:config/hazelcast.xml
# application.properties
spring.hazelcast.config=classpath:config/hazelcast.xml

5. 訪問controller

@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();

    // 調(diào)用方法
    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 開啟緩存機(jī)制.

@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 , 才會(huì)直接訪問緩存信息, 正是在MusicService 類中的方法play 上時(shí)候注解進(jìn)行過濾:
@Cacheable(condition = “#instrument.equals(‘trombone')”)

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatisPlus如何進(jìn)行連接問題

    mybatisPlus如何進(jìn)行連接問題

    MyBatisPlus提供了leftJoin方法支持左連接查詢,通過QueryWrapper構(gòu)建查詢條件,指定連接的表名和連接條件,通過select方法指定要查詢的字段
    2025-03-03
  • JavaWeb文件上傳下載實(shí)例講解(酷炫的文件上傳技術(shù))

    JavaWeb文件上傳下載實(shí)例講解(酷炫的文件上傳技術(shù))

    在Web應(yīng)用系統(tǒng)開發(fā)中,文件上傳功能是非常常用的功能,今天來主要講講JavaWeb中的文件上傳功能的相關(guān)技術(shù)實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-11-11
  • JavaFx 實(shí)現(xiàn)按鈕防抖功能

    JavaFx 實(shí)現(xiàn)按鈕防抖功能

    最近Sun公司推出了JavaFX框架,使用它可以利用JavaFX編程語言來開發(fā)富互聯(lián)網(wǎng)應(yīng)用程序(RIA),這篇文章主要介紹了JavaFx 實(shí)現(xiàn)按鈕防抖功能,需要的朋友可以參考下
    2022-01-01
  • 關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理

    關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理

    這篇文章主要介紹了關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • mybatis主從表關(guān)聯(lián)查詢,返回對(duì)象帶有集合屬性解析

    mybatis主從表關(guān)聯(lián)查詢,返回對(duì)象帶有集合屬性解析

    這篇文章主要介紹了mybatis主從表關(guān)聯(lián)查詢,返回對(duì)象帶有集合屬性解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 解決spring data jpa 批量保存更新的問題

    解決spring data jpa 批量保存更新的問題

    這篇文章主要介紹了解決spring data jpa 批量保存更新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)

    java 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)

    下面小編就為大家?guī)硪黄狫AVA 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解

    MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解

    這篇文章主要介紹了MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn),我們可以將XML中重復(fù)出現(xiàn)的內(nèi)容提取出來放到sql標(biāo)簽中,當(dāng)需要用到sql標(biāo)簽中的內(nèi)容時(shí),用include標(biāo)簽將sql標(biāo)簽中的內(nèi)容引進(jìn)來即可,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Spring的AOP極簡入門

    Spring的AOP極簡入門

    今天小編就為大家分享一篇關(guān)于Spring的AOP極簡入門,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • springboot整合activity自動(dòng)部署及部署文件命名流程

    springboot整合activity自動(dòng)部署及部署文件命名流程

    這篇文章主要介紹了springboot整合activity自動(dòng)部署及部署文件命名流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論

舞阳县| 桃江县| 吉木乃县| 海城市| 肇东市| 楚雄市| 阿荣旗| 瓦房店市| 吴江市| 建瓯市| 曲靖市| 缙云县| 秦安县| 金川县| 大余县| 津南区| 温宿县| 安龙县| 隆昌县| 绥滨县| 中超| 海林市| 安宁市| 玉田县| 宝兴县| 呼玛县| 九龙坡区| 泉州市| 桃源县| 栾城县| 崇阳县| 隆子县| 贵南县| 定襄县| 兰考县| 崇阳县| 青阳县| 德格县| 双流县| 东兴市| 永泰县|