示例#1
0
        /// <summary>
        /// Invokes the refresh action on a thread pool thread
        /// </summary>
        /// <param name="removedCacheItem">Cache item being removed. Must never be null.</param>
        /// <param name="removalReason">The reason the item was removed.</param>	
        /// <param name="instrumentationProvider">The instrumentation provider.</param>
        public static void InvokeRefreshAction(CacheItem removedCacheItem, CacheItemRemovedReason removalReason, CachingInstrumentationProvider instrumentationProvider)
        {
            if (removedCacheItem.RefreshAction == null)
            {
                return;
            }

            try
            {
                RefreshActionData refreshActionData =
                    new RefreshActionData(removedCacheItem.RefreshAction, removedCacheItem.Key, removedCacheItem.Value, removalReason, instrumentationProvider);
                refreshActionData.InvokeOnThreadPoolThread();
            }
            catch (Exception e)
            {
                instrumentationProvider.FireCacheFailed(Resources.FailureToSpawnUserSpecifiedRefreshAction, e);
            }
        }
示例#2
0
 private void RemoveItemFromCache(CacheItem itemToRemove)
 {
     lock (itemToRemove)
     {
         if (itemToRemove.WillBeExpired)
         {
             try
             {
                 cacheOperations.RemoveItemFromCache(itemToRemove.Key, CacheItemRemovedReason.Expired);
             }
             catch (Exception e)
             {
                 CachingServiceInternalFailureEvent.Fire(SR.FailureToRemoveCacheItemInBackground, e);
             }
             catch
             {
                 CachingServiceInternalFailureEvent.Fire(SR.FailureToRemoveCacheItemInBackground, new Exception(SR.UnknownFailureReason));
             }
         }
     }
 }
示例#3
0
        /// <summary>
        /// Invokes the refresh action on a thread pool thread
        /// </summary>
        /// <param name="removedCacheItem">Cache item being removed. Must never be null.</param>
        /// <param name="removalReason">The reason the item was removed.</param>
        public static void InvokeRefreshAction(CacheItem removedCacheItem, CacheItemRemovedReason removalReason)
        {
            if (removedCacheItem.RefreshAction == null)
            {
                return;
            }

            try
            {
                RefreshActionData refreshActionData =
                    new RefreshActionData(removedCacheItem.RefreshAction, removedCacheItem.Key, removedCacheItem.Value, removalReason);
                refreshActionData.InvokeOnThreadPoolThread();
            }
            catch (Exception e)
            {
                CachingServiceInternalFailureEvent.Fire(SR.FailureToSpawnUserSpecifiedRefreshAction, e);
            }
            catch
            {
                CachingServiceInternalFailureEvent.Fire(SR.FailureToSpawnUserSpecifiedRefreshAction, new Exception(SR.UnknownFailureReason));
            }
        }
示例#4
0
        private bool RemoveItemFromCache(CacheItem itemToRemove)
        {
            lock (itemToRemove)
            {
                if (itemToRemove.EligibleForScavenging)
                {
                    try
                    {
                        cacheOperations.RemoveItemFromCache(itemToRemove.Key, CacheItemRemovedReason.Scavenged);
                        return true;
                    }
                    catch (Exception e)
                    {
                        CachingServiceInternalFailureEvent.Fire(SR.FailureToRemoveCacheItemInBackground, e);
                    }
                    catch
                    {
                        CachingServiceInternalFailureEvent.Fire(SR.FailureToRemoveCacheItemInBackground, new Exception(SR.UnknownFailureReason));
                    }
                }
            }

            return false;
        }
		private bool RemoveItemFromCache(CacheItem itemToRemove)
        {
			bool 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;
        }
示例#6
0
 private static bool IsObjectInCache(CacheItem cacheItemBeforeLock)
 {
     return cacheItemBeforeLock == null || Object.ReferenceEquals(cacheItemBeforeLock.Value, addInProgressFlag);
 }
示例#7
0
		/// <summary>
		/// Add a new keyed object to the cache.
		/// </summary>
		/// <param name="key">The key of the object.</param>
		/// <param name="value">The object to add.</param>
		/// <param name="scavengingPriority">One of the <see cref="CacheItemPriority"/> values.</param>
		/// <param name="refreshAction">An <see cref="ICacheItemRefreshAction"/> object.</param>
		/// <param name="expirations">An array of <see cref="ICacheItemExpiration"/> objects.</param>
        public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            ValidateKey(key);

            CacheItem cacheItemBeforeLock = null;
            bool lockWasSuccessful = false;

            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);

                CacheItem 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);
            }  
        
        }
示例#8
0
        public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            ValidateKey(key);
            EnsureCacheInitialized();

            CacheItem cacheItemBeforeLock = null;
            bool      lockWasSuccessful   = false;

            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);

                CacheItem 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;
                }

                if (scavengingPolicy.IsScavengingNeeded(inMemoryCache.Count))
                {
                    cacheScavenger.StartScavenging();
                }

                CachingServiceItemTurnoverEvent.FireAddItems(1);
                CachingServiceItemTurnoverEvent.SetItemsTotal(inMemoryCache.Count);
            }
            finally
            {
                Monitor.Exit(cacheItemBeforeLock);
            }
        }
示例#9
0
 private bool IsObjectInCache(CacheItem cacheItemBeforeLock)
 {
     return(cacheItemBeforeLock == null || Object.ReferenceEquals(cacheItemBeforeLock.Value, addInProgressFlag));
 }
        protected override Hashtable LoadDataFromStore()
        {
            Hashtable cacheItems = new Hashtable();
            String searchString = String.Concat("*", _dataExtension);
            String[] cacheFiles = Directory.GetFiles(_filePath, searchString, SearchOption.TopDirectoryOnly);

            foreach(String cacheFile in cacheFiles)
            {
                String infoName = String.Concat(Path.GetFileNameWithoutExtension(cacheFile), _infoExtension);
                String infoPath = Path.Combine(_filePath, infoName);

                String[] infoData = File.ReadAllLines(infoPath);
                String itemKey = infoData[0];
                DateTime lastAccessed = DateTime.Parse(infoData[1]);

                TimeSpan slidingDuration;
                SlidingTime slidingTime = null;
                if(TimeSpan.TryParse(infoData[2], out slidingDuration))
                    slidingTime = new SlidingTime(slidingDuration);

                Byte[] itemBytes = File.ReadAllBytes(cacheFile);
                Object itemValue = SerializationUtility.ToObject(itemBytes);

                CacheItem item;
                if(slidingTime != null)
                    item = new CacheItem(lastAccessed, itemKey, itemValue, CacheItemPriority.Normal, null, slidingTime);
                else
                    item = new CacheItem(lastAccessed, itemKey, itemValue, CacheItemPriority.Normal, null);

                cacheItems.Add(itemKey, item);
            }

            return cacheItems;
        }
        protected override void AddNewItem(int storageKey, CacheItem newItem)
        {
            CurrentStorageKey = storageKey;

            string[] infoData = new string[3];
            infoData[0] = newItem.Key;
            infoData[1] = newItem.LastAccessedTime.ToString();

            var expirations = newItem.GetExpirations();
            if(expirations.Length > 0)
            {
                var slidingDuration = (SlidingTime)expirations.GetValue(0);
                infoData[2] = slidingDuration.ItemSlidingExpiration.ToString();
            }

            var infoFile = Path.Combine(_filePath, string.Concat(storageKey.ToString(), _infoExtension));
            try
            {
                if(File.Exists(infoFile))
                    File.Delete(infoFile);

                File.WriteAllLines(infoFile, infoData);
            }
            catch
            {
                throw new FileNotFoundException("Cannot create cache info file", infoFile);
            }

            var itemBytes = SerializationUtility.ToBytes(newItem.Value);
            var dataFile = Path.Combine(_filePath, string.Concat(storageKey.ToString(), _dataExtension));
            try
            {
                if(File.Exists(dataFile))
                    File.Delete(dataFile);

                File.WriteAllBytes(dataFile, itemBytes);
            }
            catch
            {
                throw new FileNotFoundException("Cannot create cache data file", dataFile);
            }
        }
示例#12
0
 private void InitializeExpirations(CacheItem cacheItem)
 {
     foreach (ICacheItemExpiration expiration in cacheItem.Expirations)
     {
         expiration.Initialize(cacheItem);
     }
 }
示例#13
0
        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;
        }
示例#14
0
 public void Initialize(Microsoft.Practices.EnterpriseLibrary.Caching.CacheItem owningCacheItem)
 {
     item = owningCacheItem;
 }
示例#15
0
 public void Initialize(Microsoft.Practices.EnterpriseLibrary.Caching.CacheItem owningCacheItem)
 {
     owningCacheItem.TouchedByUserAction(false);
 }
示例#16
0
文件: Cache.cs 项目: bnantz/NCS-V1-1
        public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            ValidateKey(key);
            EnsureCacheInitialized();

            CacheItem cacheItemBeforeLock = null;
            bool lockWasSuccessful = false;

            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);

                CacheItem 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;
                }

                if (scavengingPolicy.IsScavengingNeeded(inMemoryCache.Count))
                {
                    cacheScavenger.StartScavenging();
                }

                CachingServiceItemTurnoverEvent.FireAddItems(1);
                CachingServiceItemTurnoverEvent.SetItemsTotal(inMemoryCache.Count);
            }
            finally
            {
                Monitor.Exit(cacheItemBeforeLock);
            }
        }