Как я могу заблокировать ключ кеша?

Я пытаюсь реализовать универсальный потокобезопасный метод Cache, и мне интересно, как мне реализовать в нем блокировку.

] Это должно выглядеть примерно так:

//private static readonly lockObject = new Object();

public T GetCache<T>(string key, Func<T> valueFactory...)
{

  // try to pull from cache here

  lock (lockObject) // I don't want to use static object lock here because then every time a lock is performed, all cached objects in my site have to wait, regarding of the cache key.
  {
    // cache was empty before we got the lock, check again inside the lock

    // cache is still empty, so retreive the value here

    // store the value in the cache here
  }

  // return the cached value here

}
14
задан wonea 7 February 2018 в 11:49
поделиться