コード例 #1
0
ファイル: NHashtable.cs プロジェクト: ddd-cqrs-es/must
 public void Add(string key, object value, TimeSpan slidingExpiration, CacheItemRemovedCallback cacheRemove)
 {
     string cacheKey = CacheKey(key, _hashkey);
     lock (GetLock(cacheKey, TimeSpan.Zero, slidingExpiration, null, this))
     {
         object local = NCache.Get(cacheKey);
         if (local == null)
             local = NHashtable.InternalCallback(cacheKey);
     }
 }
コード例 #2
0
ファイル: NHashtable.cs プロジェクト: ddd-cqrs-es/must
        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;
        }
コード例 #3
0
ファイル: NHashtable.cs プロジェクト: ddd-cqrs-es/must
        /// <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;
        }
コード例 #4
0
ファイル: NHashtable.cs プロジェクト: ddd-cqrs-es/must
        /// <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);
                    }
                }
            }
        }
コード例 #5
0
ファイル: NHashtable.cs プロジェクト: ddd-cqrs-es/must
        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;
        }