Using 'HttpContext.Current.Cache' safely

前端 未结 3 1981
小蘑菇
小蘑菇 2021-02-13 06:57

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:38

    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...

提交回复
热议问题