internal static object GetLock(string key, TimeSpan refreshInterval, TimeSpan slidingExpiration, CacheItemRemovedCallback cacehRemoved, NHashtable table) { HashCacheEntry entry = new HashCacheEntry(); entry.RefreshInterval = refreshInterval; entry.SlidingExpiration = slidingExpiration; entry.LastUse = DateTime.Now; entry.CacheRemoved = cacehRemoved; entry.RefTable = table; CacheLockbox.Add(key, entry); return entry.Locker; }
/// <summary> /// Removes the element with the specified key from the NHashtable /// </summary> /// <param name="key">The key of the element to remove</param> public void Remove(string key) { string cacheKey = CacheKey(key, _hashkey); CacheEntry entry = null; if (CacheLockbox.TryGetCacheEntry(cacheKey, out entry)) { lock (CacheLockbox.GetLock(cacheKey)) { HashCacheEntry hashEntry = (HashCacheEntry)entry; if (hashEntry.CacheRemoved != null) hashEntry.CacheRemoved(key, base[cacheKey], CacheItemRemovedReason.Removed); NCache.Remove(cacheKey); } } base.Remove(cacheKey); }
public object Get(string key) { string cacheKey = NHashtable.CacheKey(key, _hashkey); object obj = base[cacheKey]; CacheEntry entry = null; if (CacheLockbox.TryGetCacheEntry(cacheKey, out entry)) { lock (CacheLockbox.GetLock(cacheKey)) { object local = NCache.Get(cacheKey); if (local == null) local = NHashtable.InternalCallback(cacheKey); return local; } } return obj; }
/// <summary> /// Callback function used to re-insert a expired cached object into the cache /// </summary> /// <param name="key">The key used to put the object on the cache</param> internal static object InternalCallback(string cacheKey) { CacheEntry cacheEntry = null; if (!CacheLockbox.TryGetCacheEntry(cacheKey, out cacheEntry)) return null; object obj = null; NHashtable nHash = ((HashCacheEntry)cacheEntry).RefTable; if (nHash != null) { obj = nHash[cacheKey]; if(obj != null) NCache.Insert(cacheKey, obj, null, (int)cacheEntry.SlidingExpiration.TotalSeconds, CacheItemPriority.Normal, new CacheItemRemovedCallback(NHashtable.ItemRemovedCallback)); } // signal the CacheLockbox about the usage of the object CacheLockbox.UpdateCacheEntry(cacheKey, DateTime.Now); return obj; }
/// <summary> /// Callback method called when a object is removed for the ASP.NET cache. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="reason">The reason an item was removed from the ASP.NET cache</param> internal static void ItemRemovedCallback(string cacheKey, object value, CacheItemRemovedReason reason) { if (reason == CacheItemRemovedReason.Expired) { int num = cacheKey.IndexOf('-'); string key = cacheKey.Substring(0, num); CacheEntry cacheEntry = null; if (CacheLockbox.TryGetCacheEntry(cacheKey, out cacheEntry)) { if (cacheEntry.LastUse.Add(cacheEntry.SlidingExpiration) > DateTime.Now) { NCache.cache_.Insert(cacheKey, value, null, DateTime.Now.Add(TimeSpan.FromSeconds(30.0)), TimeSpan.Zero, CacheItemPriority.Low, null); ThreadPool.QueueUserWorkItem(delegate(object o) { string k = o.ToString(); lock (CacheLockbox.GetInternalLock(k)) { InternalCallback(k); } }, cacheKey); } else { lock (CacheLockbox.GetLock(cacheKey)) { HashCacheEntry hashEntry = (HashCacheEntry)cacheEntry; if (hashEntry.CacheRemoved != null) hashEntry.CacheRemoved(key, value, reason); NHashtable table = hashEntry.RefTable; if (table != null) table.Remove(cacheKey); } CacheLockbox.Remove(cacheKey); } } } }