Using 'HttpContext.Current.Cache' safely

前端 未结 3 761
日久生厌
日久生厌 2021-02-13 07:12

I am using Cache in a web service method like this:

var pblDataList = (List)HttpContext.Current.Cache.Get(\"pblDataList\");

if (pblDa         


        
3条回答
  •  清酒与你
    2021-02-13 07:56

    I believe the Add should be thread-safe - i.e. it won't error if Add gets called twice with the same key, but obviously the query might execute twice.

    Another question, however, is is the data thread-safe. There is no guarantee that each List is isolated - it depends on the cache-provider. The in-memory cache provider stores the objects directly, so there is a risk of collisions if any of the threads edit the data (add/remove/swap items in the list, or change properties of one of the items). However, with a serializing provider you should be fine. Of course, this then demands that blabla is serializable...

提交回复
热议问题