public T GetOrAdd(string key, Func <T> loadFunction) { LazyLock lazy; bool success; synclock.EnterReadLock(); try { success = this.TryGetValue(key, out lazy); } finally { synclock.ExitReadLock(); } if (!success) { synclock.EnterWriteLock(); try { if (!this.TryGetValue(key, out lazy)) { lazy = new LazyLock(); this.Add(key, lazy); } } finally { synclock.ExitWriteLock(); } } return(lazy.Get(loadFunction)); }
private bool TryGetValue(string key, out LazyLock value) { value = this.Get(key); if (value != null) { return(true); } return(false); }
private void Add(string key, LazyLock item) { var policy = new CacheItemPolicy(); policy.Priority = CacheItemPriority.NotRemovable; if (IsTimespanSet(cachePolicy.AbsoluteExpiration)) { policy.AbsoluteExpiration = DateTimeOffset.Now.Add(cachePolicy.AbsoluteExpiration); } else if (IsTimespanSet(cachePolicy.SlidingExpiration)) { policy.SlidingExpiration = cachePolicy.SlidingExpiration; } cache.Add(key, item, policy); }