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