title: c# lock in generic function
body: <p>I have such a class:</p>
  
  <pre><code>public static class CacheManager
  {
      static object lockObject = new object();
  
      static MemcachedClient CacheObject
      {
          get
          {
              if (!MemcachedClient.Exists(Settings.Default
                  .CacheInstanceName))
              {
                  MemcachedClient.Setup(Settings.Default
                      .CacheInstanceName,
                          new string[] {Settings.Default
                              .CacheHostAddress});
              }
              //
              //
              return MemcachedClient.GetInstance(Settings
                  .Default.CacheInstanceName);
          }
      }
  
      public static List&lt;TData&gt; Get&lt;TData&gt;(string key, Func&lt;List&lt;int&gt;&gt; getListCallback,
          Func&lt;int, TData&gt; getItemCallback) where TData : class
      {
          var result = new List&lt;TData&gt;();
          //
          //
          var list = CacheObject.Get(key);
          if (list == null)
          {
              lock (lockObject)
              {
                  list = CacheObject.Get(key);
                  if (list == null)
                  {
                      list = getListCallback();
                      CacheObject.Set(key, list);
                      //
                      //
                      foreach (var id in (List&lt;int&gt;)list)
                      {
                          var item = getItemCallback(id);
                          result.Add(item);
                          CacheObject.Set(string.Concat(key, id), item);
                      }
                  }
              }
          }
          else
          {
              foreach (var id in (List&lt;int&gt;)list)
              {
                  var itemKey = string.Concat(key, id);
                  //
                  //
                  var item = CacheObject.Get(itemKey);
                  if (item == null)
                  {
                      lock (lockObject)
                      {
                          item = CacheObject.Get(itemKey);
                          if (item == null)
                          {
                              item = getItemCallback(id);
                              CacheObject.Set(itemKey, item);
                          }
                      }
                  }
                  //
                  //
                  result.Add((TData)item);
              }
          }
          //
          //
          return (List&lt;TData&gt;)result;
      }
  
      public static void Remove(string key)
      {
          CacheObject.Delete(key);
      }
  }
  </code></pre>
  
  <p>it is used in classes-repositories:</p>
  
  <pre><code>public class NewsRepository : BaseRepository, IRepository
  {
  
      public List&lt;News&gt; FindAll()
      {
          return CacheManager.Get&lt;News&gt;(key,
              () =&gt; clientEntities.News.OrderByDescending(n =&gt; n.DateCreated).Select(n =&gt; n.NewsId).ToList(),
              (id) =&gt; clientEntities.News.Single(n =&gt; n.NewsId == id));
      }
  }
  public class PagesRepository : BaseRepository
  {
      public List&lt;Page&gt; FindAll()
      {
  
          return CacheManager.Get&lt;Page&gt;(key,
              () =&gt; clientEntities.Pages.OrderBy(p =&gt; p.PageId).Select(p =&gt; p.PageId).ToList(),
              (id) =&gt; clientEntities.Pages.Single(p =&gt; p.PageId == id));
      }
  }
  </code></pre>
  
  <p>my question is: for example NewsRepository didn't find news in cache and got the lock and began to load data but at this moment PagesRepository didn't find pages in cache. will PagesRepository's CacheManager be locked by NewsRepository or (I think so) NewsRepository's CacheManager is another static class and its internal locks do not touch PagesRepository's CacheManager?</p>