springboot hazelcast緩存中間件的實例代碼
緩存來了
在dotnet平臺有自己的緩存框架,在java springboot里當(dāng)然了集成了很多,而且緩存的中間件也可以進(jìn)行多種選擇,向 redis , hazelcast 都是分布式的緩存中間件,今天主要說一下后者的實現(xiàn)。
添加依賴包
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統(tǒng)一配置
@Configuration
@Profile("hazelcast-cache")//運行環(huán)境名稱
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;
}
}
添加倉儲
public interface UserRepository {
List<UserInfo> fetchAllUsers();
List<UserInfo> fetchAllUsers(String name);
}
@Repository
@Profile("hazelcast-cache")// 指定在這個hazelcast-cache環(huán)境下,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}") // 方法名和參數(shù)組合做為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開啟這個緩存的環(huán)境
profiles.active: hazelcast-cache
運行程序
可以在單元測試?yán)镞M(jìn)行測試,調(diào)用多次,方法體只進(jìn)入一次,這就是緩存成功了。
@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();
}
}
總結(jié)
以上所述是小編給大家介紹的springboot hazelcast緩存中間件的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Cloud入門系列服務(wù)提供者總結(jié)
這篇文章主要介紹了Spring Cloud入門系列之服務(wù)提供者總結(jié),服務(wù)提供者使用Eureka Client組件創(chuàng)建 ,創(chuàng)建完成以后修改某文件,具體操作方法及實例代碼跟隨小編一起看看吧2021-06-06
String.intern()作用與常量池關(guān)系示例解析
這篇文章主要為大家介紹了String.intern()作用與常量池關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
SpringCloud?中防止繞過網(wǎng)關(guān)請求直接訪問后端服務(wù)的解決方法
這篇文章主要介紹了SpringCloud中如何防止繞過網(wǎng)關(guān)請求直接訪問后端服務(wù),本文給大家分享三種解決方案,需要的朋友可以參考下2023-06-06

