private CachedItem executeAndPutInCache(KeyAndItsDependingKeys keyAndItsDependingKeys, CachedMethodInformation cachedMethodInformation, Func <object> originalMethod) { var methodResult = originalMethod(); var cachedItem = new CachedItem(cachedMethodInformation, methodResult); var key = keyAndItsDependingKeys.Key; var dependedKeys = keyAndItsDependingKeys.DependingRemoveKeys().ToList(); dependedKeys.Add(mainCacheKey); createDependencies(dependedKeys); var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(_timeoutMinutes), RemovedCallback = arguments => _eventListenersCallback.OnCacheRemoval(cachedItem) }; policy.ChangeMonitors.Add(cache.CreateCacheEntryChangeMonitor(dependedKeys)); cache.Set(key, cachedItem, policy); return(cachedItem); }
public object GetAndPutIfNonExisting(KeyAndItsDependingKeys keyAndItsDependingKeys, CachedMethodInformation cachedMethodInformation, Func <object> originalMethod) { var cachedItem = (CachedItem)cache.Get(keyAndItsDependingKeys.Key); if (cachedItem != null) { _eventListenersCallback.OnCacheHit(cachedItem); return(cachedItem.CachedValue); } lock (lockObject) { var cachedItem2 = (CachedItem)cache.Get(keyAndItsDependingKeys.Key); if (cachedItem2 != null) { _eventListenersCallback.OnCacheHit(cachedItem2); return(cachedItem2.CachedValue); } var addedValue = executeAndPutInCache(keyAndItsDependingKeys, cachedMethodInformation, originalMethod); _eventListenersCallback.OnCacheMiss(addedValue); return(addedValue.CachedValue); } }