Are there any nasty side affects if i lock the HttpContext.Current.Cache.Insert method

Posted by Ekk on Stack Overflow See other posts from Stack Overflow or by Ekk
Published on 2010-05-04T03:39:39Z Indexed on 2010/05/04 3:48 UTC
Read the original article Hit count: 281

Filed under:
|
|

Apart from blocking other threads reading from the cache what other problems should I be thinking about when locking the cache insert method for a public facing website.

The actual data retrieval and insert into the cache should take no more than 1 second, which we can live with. More importantly i don't want multiple thread potentially all hitting the Insert method at the same time.

The sample code looks something like:

public static readonly object _syncRoot = new object();

if (HttpContext.Current.Cache["key"] == null)
{
  lock (_syncRoot)
  {
    HttpContext.Current.Cache.Insert("key", "DATA", null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
  }
}

  Response.Write(HttpContext.Current.Cache["key"]);

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about caching