Safe HttpContext.Current.Cache Usage

Posted by Burak SARICA on Stack Overflow See other posts from Stack Overflow or by Burak SARICA
Published on 2010-04-14T14:47:44Z Indexed on 2010/04/14 15:33 UTC
Read the original article Hit count: 305

Filed under:
|
|
|

Hello there, I use Cache in a web service method like this :

var pblDataList = (List<blabla>)HttpContext.Current.Cache.Get("pblDataList");
            if (pblDataList == null)
            {
                var PBLData = dc.ExecuteQuery<blabla>(
    @"SELECT blabla");

                pblDataList = PBLData.ToList();

                HttpContext.Current.Cache.Add("pblDataList", pblDataList, null, DateTime.Now.Add(new TimeSpan(0, 0, 15)), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

            }

I wonder is it thread safe? I mean the method is called by multiple requesters And more then one requester may hit the second line at the same time while the cache is empty. So all of these requesters will retrieve the data and add to cache. The query takes 5-8 seconds. May a surrounding lock statement around this code prevent that action? (I know multiple queries will not cause error but i want to be sure running just one query.)

© Stack Overflow or respective owner

Related posts about c#

Related posts about web