/// <summary> /// Вызов рефреша в нити /// </summary> /// <param name="removedCacheItem">Удаляемый кэш-элемент. Не null.</param> /// <param name="removalReason">Причина удаления.</param> /// <param name="instrumentationProvider"></param> public static void InvokeRefreshAction(CacheItem removedCacheItem, CacheItemRemovedReason removalReason, ICachingInstrumentationProvider instrumentationProvider) { if (removedCacheItem == null) throw new ArgumentNullException("removedCacheItem"); if (instrumentationProvider == null) throw new ArgumentNullException("instrumentationProvider"); if (removedCacheItem.RefreshAction == null) { return; } try { var refreshActionData = new RefreshActionData(removedCacheItem.RefreshAction, removedCacheItem.Key, removedCacheItem.Value, removalReason, instrumentationProvider); refreshActionData.InvokeOnThreadPoolThread(); } catch (Exception e) { instrumentationProvider.FireCacheFailed(Resources.FailureToSpawnUserSpecifiedRefreshAction, e); } }
/// <summary> /// Добавление объекта в кэш /// </summary> /// <param name="value"></param> /// <param name="scavengingPriority">Приоритет.</param> /// <param name="refreshAction">Действие рефреша.</param> /// <param name="expirations">Ограничения на нахождение элемента в кэше.</param> /// <param name="key"></param> public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations) { ValidateKey(key); CacheItem cacheItemBeforeLock; bool lockWasSuccessful; do { lock (inMemoryCache.SyncRoot) { if (inMemoryCache.Contains(key) == false) { cacheItemBeforeLock = new CacheItem(key, AddInProgressFlag, CacheItemPriority.NotRemovable, null); inMemoryCache[key] = cacheItemBeforeLock; } else { cacheItemBeforeLock = (CacheItem)inMemoryCache[key]; } lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock); } if (lockWasSuccessful == false) { Thread.Sleep(0); } } while (lockWasSuccessful == false); try { cacheItemBeforeLock.TouchedByUserAction(true); var newCacheItem = new CacheItem(key, value, scavengingPriority, refreshAction, expirations); try { backingStore.Add(newCacheItem); cacheItemBeforeLock.Replace(value, refreshAction, scavengingPriority, expirations); inMemoryCache[key] = cacheItemBeforeLock; } catch { backingStore.Remove(key); inMemoryCache.Remove(key); throw; } instrumentationProvider.FireCacheUpdated(1, inMemoryCache.Count); } finally { Monitor.Exit(cacheItemBeforeLock); } }
private static bool IsObjectInCache(CacheItem cacheItemBeforeLock) { return cacheItemBeforeLock == null || ReferenceEquals(cacheItemBeforeLock.Value, AddInProgressFlag); }
private bool RemoveItemFromCache(CacheItem itemToRemove) { var expired = false; lock (itemToRemove) { if (itemToRemove.WillBeExpired) { try { expired = true; cacheOperations.RemoveItemFromCache(itemToRemove.Key, CacheItemRemovedReason.Expired); } catch (Exception e) { instrumentationProvider.FireCacheFailed(Resources.FailureToRemoveCacheItemInBackground, e); } } } return expired; }
private bool RemoveItemFromCache(CacheItem itemToRemove) { lock (itemToRemove) { if (itemToRemove.EligibleForScavenging) { try { cacheOperations.RemoveItemFromCache(itemToRemove.Key, CacheItemRemovedReason.Scavenged); return true; } catch (Exception e) { instrumentationProvider.FireCacheFailed(Resources.FailureToRemoveCacheItemInBackground, e); } } } return false; }