A question about Cache.cs

Topics: Developer Forum
Jul 6, 2009 at 3:17 AM

public byte[] Get(string key)
  {
   byte[] o = null;
   try
   {
    lock (dict)
    {
     if (dict.ContainsKey(key))
     {
      o = dict[key];
     }
     else
      o = null;
    }
   }
   catch (Exception ex)
   {
    Handler.LogHandler.Info(@"Get Failed!", ex);
   }
   return o;
  }

 

I've seen code above in Cache.cs, and I've got a question: why we need a lock here? Only for the synchronization with insert/update/remove method? Why not using ReaderWriterLock here? Thanks a lot!

Coordinator
Jul 6, 2009 at 8:29 AM

I think you are right a ReaderWriterLock would make more sense over here - there is no specific reason why I have choosen to use a normal lock.

regards, roni