public T BlitzGet<T>(string cacheKey, Func<T> function, long? milliseconds = null) { if (memoryCache.TryGetValue(cacheKey, out T result)) return result; lock (LockDictionary.Get(cacheKey)) { if (memoryCache.TryGetValue(cacheKey, out result)) return result; result = function.Invoke(); memoryCache.Set(cacheKey, result, DateTime.Now.AddMilliseconds(milliseconds ?? defaultMilliseconds)); } return result; }
public void LockDictionaryPerformance() { var start = DateTime.Now; Parallel.For(0, numberOfTests, (i) => { LockDictionary.Get(Guid.NewGuid().ToString()); }); var elapsed = (DateTime.Now - start).TotalMilliseconds; Assert.AreEqual(numberOfTests, LockDictionary.GetNumberOfLocks()); }
public void Remove(string cacheKey) { lock (LockDictionary.Get(cacheKey)) memoryCache.Remove(cacheKey); }
public void BlitzUpdate<T>(string cacheKey, Func<T> function, long milliseconds) { lock (LockDictionary.Get(cacheKey)) memoryCache.Set(cacheKey, function(), DateTime.Now.AddMilliseconds(milliseconds)); }