當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
java B2B2C Springcloud电子商务平台源码------Hystrix的缓存使用
生活随笔
收集整理的這篇文章主要介紹了
java B2B2C Springcloud电子商务平台源码------Hystrix的缓存使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一?介紹
在高并發(fā)的場景之下,Hystrix中提供了請求緩存的功能,可以方便地開啟和使用請求緩存來優(yōu)化系統(tǒng),達到減輕高并發(fā)時請求線程的消耗、降低請求響應(yīng)時間的效果。愿意了解源碼的朋友直接求求交流分享技術(shù):二一四七七七五六三三
二開啟請求緩存功能
在實現(xiàn)HystrixCommand或HystrixObservableCommand時,通過重載getCacheKey()方法來開啟請求緩存。
例如:
public class CommandUsingRequestCache extends HystrixCommand<Boolean> {private final int value;protected CommandUsingRequestCache(int value) {super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));this.value = value;}@Overrideprotected Boolean run() {return value == 0 || value % 2 == 0;}//通過getCacheKey方法中返回的請求緩存key值,就能讓該請求命令具備緩存功能。此時當不同的外部請求//處理邏輯調(diào)用了同一個依賴服務(wù)時,Hystrix會根據(jù)getCacheKey方法返回的值區(qū)分是否是重復(fù)請求,//如果它們的cachekey相同時候,那么該依賴服務(wù)值會在第一個請求達到時被真實的調(diào)用一次,另外一個//請求則直接從請求緩存中返回結(jié)果,所以開啟緩存有以下好處://減少重復(fù)請求數(shù),降低依賴服務(wù)的并發(fā)度//在同一用戶請求的上下文中,相同依賴服務(wù)的返回數(shù)據(jù)始終保持一致。//請求緩存在run()和construct()執(zhí)行之前生效,所以可以有效減少不必要的線程開銷。@Overrideprotected String getCacheKey() {return String.valueOf(value);} } 復(fù)制代碼三?清理失效緩存功能
使用請求緩存時,如果只是讀操作,那么不需要考慮緩存內(nèi)容是否正確的問題,但是如果請求命令中還有更新數(shù)據(jù)的操作,那么緩存中的數(shù)據(jù)就需要我們在進行寫操作時進行及時處理,以防止讀操作的請求命令獲取到失效的數(shù)據(jù)。
在Hystrix中,可以通過HystrixRequestCache.clear()方法來進行緩存的清理。
例如:
//當我們對GetterCommand命令實現(xiàn)了請求緩存之后,那么勢必需要為SetterCommand命令實現(xiàn)清理緩存,以保證 //prefixStoredOnRemoteDataStore被更新之后,Hystrix請求緩存中相同的緩存的結(jié)果被移除,這樣下一次根據(jù)id //獲取prefixStoredOnRemoteDataStore時,不會從緩存去獲取數(shù)據(jù) public class CommandUsingRequestCacheInvalidation {/* represents a remote data store */private static volatile String prefixStoredOnRemoteDataStore = "ValueBeforeSet_";//根據(jù)id獲取數(shù)據(jù)public static class GetterCommand extends HystrixCommand<String> {private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("GetterCommand");private final int id;public GetterCommand(int id) {super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GetSetGet")).andCommandKey(GETTER_KEY));this.id = id;}@Overrideprotected String run() {return prefixStoredOnRemoteDataStore + id;}@Overrideprotected String getCacheKey() {return String.valueOf(id);}//該方法從默認的Hystrix并發(fā)策略中根據(jù)GETTER_KEY獲取命令的請求緩存對象HystrixRequestCache的實例//然后再調(diào)用該請求緩存對象的clear方法,對Key為id值的緩存內(nèi)容進行清理。public static void flushCache(int id) {HystrixRequestCache.getInstance(GETTER_KEY,HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(id));}}//用于更新prefixStoredOnRemoteDataStore的值public static class SetterCommand extends HystrixCommand<Void> {private final int id;private final String prefix;public SetterCommand(int id, String prefix) {super(HystrixCommandGroupKey.Factory.asKey("GetSetGet"));this.id = id;this.prefix = prefix;}@Overrideprotected Void run() {// persist the value against the datastoreprefixStoredOnRemoteDataStore = prefix;//在調(diào)用了寫prefixStoredOnRemoteDataStore之后,增加了對GetterCommand//中靜態(tài)方法flushCache的調(diào)用,以實現(xiàn)對時效緩存的清理工作。GetterCommand.flushCache(id);// no return valuereturn null;}} } 復(fù)制代碼整體代碼結(jié)構(gòu)如下: 資料和源碼來源
轉(zhuǎn)載于:https://juejin.im/post/5c09e045f265da617006eecc
總結(jié)
以上是生活随笔為你收集整理的java B2B2C Springcloud电子商务平台源码------Hystrix的缓存使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 布隆过滤器误判怎么办为什么会_说一说布隆
- 下一篇: java B2B2C源码电子商务平台 -