簡體   English   中英

如何在響應列表中為輸入列表緩存每個元素

[英]How to cache each element in List of responses for list of inputs

我有一個函數,它使用一組輸入調用外部系統。 然后,后端系統將匯總所有輸入的結果。

稍后,我可能需要使用單個輸入來調用該系統。 我想避免這種情況,因為我之前已經得到了輸入組的響應。

我想利用其他系統的集體響應來實現緩存。

public List<Result> process(List<Input> inputs){
// call other system collectively and cache each independent response
// later when I call each input it shall not cal external system because we have cache.

 foreach(Result result: results){
 addtoCache(input, result);

 }

}

@CacheResult(cacheName = "abc")
public Result addToCache(@CacheKey Input input, Result result){
return result;
}

@CacheResult(cacheName = "abc")
public Result getResult(@CacheKey Input input){
//process and send result. It shall not call the external system if the data was obtained previously in process method.
}

在以后的時間

我將調用getResult方法,如果可用,它將獲取在addToCache方法下緩存的緩存。

如果這是正確的實施方式,可以有人幫我。

注釋應該緩存該方法的返回對象(在addToCache方法的情況下為result )。 此處代碼中的兩種方法都修改了同一緩存,我認為這是不必要的。

最好只有一個方法,您最終要調用該方法,並用批注修飾它,然后可以省去另一個方法。 每當您調用getResult方法時,都將進行緩存,它將負責填充緩存,如果未緩存,則將調用外部系統。 確保在getResult方法中添加了實際的實現,即在該方法內部對外部系統的調用。 如果緩存中已經包含對象(緩存中的搜索將基於您已定義的鍵),它將確保不會調用外部系統。

@CacheResult(cacheName = "abc")
public Result getResult(@CacheKey Input input){
//Call the actual service or external system here. 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM