Caching asynchronous operations

后端 未结 6 489
暖寄归人
暖寄归人 2020-12-28 16:40

I am looking for an elegant way of caching the results of my asynchronous operations.

I first had a synchronous method like this:

public String GetSt         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-28 17:35

    I have made an wrapper for MemoryCache that basically caches Lazy> objects and works so that all the following problems are solved:

    • No parallel or unnecessary operations to get a value will be started. Multiple call sites or threads could await for the same value from the cache.
    • Failed Tasks are not cached. (No negative caching.)
    • Cache users can't get invalidated results from the cache, even if the value is invalidated during an await.

    The solution is further explained in my blog and the full working code is available at GitHub.

提交回复
热议问题