日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

HazelCast的Spring-Boot和Cache抽象

發(fā)布時(shí)間:2023/12/3 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HazelCast的Spring-Boot和Cache抽象 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以前,我們是使用spring提供的默認(rèn)Cache Manager來開始Spring Cache抽象的。

盡管這種方法可能適合我們對簡單應(yīng)用程序的需求,但是在出現(xiàn)復(fù)雜問題的情況下,我們需要使用具有更多功能的其他工具。 Hazelcast就是其中之一。 當(dāng)涉及到基于JVM的應(yīng)用程序時(shí),Hazelcast是一個(gè)很好的緩存工具。 通過使用hazelcast作為緩存,數(shù)據(jù)可以在計(jì)算機(jī)群集的節(jié)點(diǎn)之間平均分配,從而可以對可用存儲(chǔ)進(jìn)行水平擴(kuò)展。

我們將使用spring配置文件運(yùn)行代碼庫,因此“ hazelcast-cache”將是我們的配置文件名稱。

group 'com.gkatzioura' version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")} }apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot'repositories {mavenCentral() }sourceCompatibility = 1.8 targetCompatibility = 1.8dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile("org.springframework.boot:spring-boot-starter-cache")compile("org.springframework.boot:spring-boot-starter")compile("com.hazelcast:hazelcast:3.7.4")compile("com.hazelcast:hazelcast-spring:3.7.4")testCompile("junit:junit") }bootRun {systemProperty "spring.profiles.active", "hazelcast-cache" }

如您所見,我們更新了上一個(gè)示例中的gradle文件,并添加了兩個(gè)額外的依賴項(xiàng)hazelcast和hazelcast-spring。 此外,我們還更改了應(yīng)用程序默認(rèn)運(yùn)行的配置文件。

我們的下一步是配置hazelcast緩存管理器。

package com.gkatzioura.caching.config;import com.hazelcast.config.Config; import com.hazelcast.config.EvictionPolicy; import com.hazelcast.config.MapConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile;/*** Created by gkatzioura on 1/10/17.*/ @Configuration @Profile("hazelcast-cache") public class HazelcastCacheConfig {@Beanpublic Config hazelCastConfig() {Config config = new Config();config.setInstanceName("hazelcast-cache");MapConfig allUsersCache = new MapConfig();allUsersCache.setTimeToLiveSeconds(20);allUsersCache.setEvictionPolicy(EvictionPolicy.LFU);config.getMapConfigs().put("alluserscache",allUsersCache);MapConfig usercache = new MapConfig();usercache.setTimeToLiveSeconds(20);usercache.setEvictionPolicy(EvictionPolicy.LFU);config.getMapConfigs().put("usercache",usercache);return config;}}

我們剛剛創(chuàng)建了兩個(gè)具有20秒ttl策略的地圖。 因此,自填充地圖以來20秒,將發(fā)生緩存逐出。 有關(guān)更多的hazelcast配置,請參閱官方的hazelcast 文檔 。

我們必須實(shí)現(xiàn)的另一項(xiàng)更改是將UserPayload更改為可序列化的Java對象,因?yàn)榇鎯?chǔ)在hazelcast中的對象必須是可序列化的。

package com.gkatzioura.caching.model;import java.io.Serializable;/*** Created by gkatzioura on 1/5/17.*/ public class UserPayload implements Serializable {private String userName;private String firstName;private String lastName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;} }

最后但并非最不重要的一點(diǎn)是,我們添加了另一個(gè)綁定到hazelcast-cache配置文件的存儲(chǔ)庫。

結(jié)果就是我們先前的與hazelcast集成的spring-boot應(yīng)用程序,而不是使用ttl策略配置的默認(rèn)緩存。

您可以在github上找到源代碼。

翻譯自: https://www.javacodegeeks.com/2017/01/spring-boot-cache-abstraction-hazelcast.html

總結(jié)

以上是生活随笔為你收集整理的HazelCast的Spring-Boot和Cache抽象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。