/// <summary> /// Constructor /// </summary> /// <param name="cache"></param> /// <param name="keyList"></param> /// <param name="bAllowNulls"></param> public LazyKeysetEnumerator(CacheBase cache, object[] keyList, bool bAllowNulls) { _cache = cache; _keyList = keyList; _bAllowNulls = bAllowNulls; ((IEnumerator)this).Reset(); }
/// <summary> /// Tells if the cache is a clustered cache or a local one /// </summary> /// <param name="cache"></param> /// <returns></returns> #if !CLIENT public static bool IsClusteredCache(CacheBase cache) { if (cache == null) return false; else return cache.GetType().IsSubclassOf(typeof(ClusterCacheBase)); }
/// <summary> /// Default constructor. /// </summary> public LocalCacheImpl(CacheBase cache) { if (cache == null) throw new ArgumentNullException("cache"); _cache = cache; _context = cache.InternalCache.Context; }
/// <summary> /// Default constructor. /// </summary> public CacheSyncWrapper(CacheBase cache) { if (cache == null) throw new ArgumentNullException("cache"); _cache = cache; _context = cache.Context; _syncObj = _cache.Sync; }
public void Initialize(EnumerationPointer pointer, IndexedLocalCache cache) { _cache = cache; _pointer = pointer; if (System.Configuration.ConfigurationSettings.AppSettings.Get("NCacheServer.EnumeratorChunkSize") != null) _chunkSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings.Get("NCacheServer.EnumeratorChunkSize")); _snapshot = CacheSnapshotPool.Instance.GetSnaphot(pointer.Id, cache); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or /// resetting unmanaged resources. /// </summary> public override void Dispose() { if (_cache != null) { _cache.Dispose(); _cache = null; } base.Dispose(); }
public void Execute(CacheBase cache, CacheRuntimeContext context, long count) { Sync.AcquireWriterLock(Timeout.Infinite); try { _evctPolicy.Execute(cache,context, count); } finally { Sync.ReleaseWriterLock(); } }
/// <summary> /// Returns the thread safe synchronized wrapper over cache. /// </summary> /// <param name="cacheStore"></param> /// <returns></returns> public static CacheBase Synchronized(CacheBase cache) { return(new CacheSyncWrapper(cache)); }
/// <summary> /// Returns the number of items in local instance of the cache. /// </summary> /// <param name="cache"></param> /// <returns></returns> public static long GetLocalCount(CacheBase cache) { if(cache != null && cache.InternalCache != null) return (long)cache.InternalCache.Count; return 0; }
/// <summary> /// Get a snaphot from the pool /// </summary> /// <param name="pointerID"></param> /// <param name="cache"></param> /// <returns></returns> public Array GetSnaphot(string pointerID, CacheBase cache) { CachePool pool = null; if (_cachePoolMap.Contains(cache.Context.CacheRoot.Name)) { pool = _cachePoolMap[cache.Context.CacheRoot.Name] as CachePool; return pool.GetSnaphotInPool(pointerID, cache); } else { pool = new CachePool(_minSnaphotSizeForPooling, _maxSnapshotsInPool, _newSnapshotCreationThreshold); _cachePoolMap.Add(cache.Context.CacheRoot.Name, pool); } return pool.GetSnaphotInPool(pointerID, cache); }
/// <summary> /// Return a snaphot from the snaphot pool to be used by current enumerator /// </summary> /// <param name="pointerID">unque id of the enumeration pointer being used by current enumerator.</param> /// <param name="cache">underlying cache from which snapshot has to be taken.</param> /// <returns>snaphot as an array.</returns> public Array GetSnaphotInPool(string pointerID, CacheBase cache) { string uniqueID = string.Empty; if (cache.Count < _minimumSnaphotSizeForPooling) { return cache.Keys; } else { if (_pool.Count == 0) { uniqueID = GetNewUniqueID(); _pool.Add(uniqueID, cache.Keys); _lastSnaphotCreationTime = DateTime.Now; _currentUsableSnapshot = uniqueID; } else if (_pool.Count < _maxNumOfSnapshotsInPool) { TimeSpan elapsedTime = DateTime.Now.Subtract(_lastSnaphotCreationTime); if (elapsedTime.TotalSeconds >= _newSnapshotCreationTimeInSec) { uniqueID = GetNewUniqueID(); _pool.Add(uniqueID, cache.Keys); _lastSnaphotCreationTime = DateTime.Now; _currentUsableSnapshot = uniqueID; } } if (!_enumeratorSnaphotMap.ContainsKey(pointerID)) { _enumeratorSnaphotMap.Add(pointerID, uniqueID); if (!_snapshotRefCountMap.ContainsKey(uniqueID)) { _snapshotRefCountMap.Add(uniqueID, 1); } else { int refCount = _snapshotRefCountMap[uniqueID]; refCount++; _snapshotRefCountMap[uniqueID] = refCount; } } return _pool[_currentUsableSnapshot]; } }
/// <summary> /// Get a snaphot from the pool for a particular cache. /// </summary> public void DiposeSnapshot(string pointerID, CacheBase cache) { CachePool pool = null; if (_cachePoolMap.Contains(cache.Context.CacheRoot.Name)) { pool = _cachePoolMap[cache.Context.CacheRoot.Name] as CachePool; pool.DiposeSnapshotInPool(pointerID); } }
/// <summary> /// Returns the thread safe synchronized wrapper over cache. /// </summary> /// <param name="cacheStore"></param> /// <returns></returns> public static CacheBase Synchronized(CacheBase cache) { return new CacheSyncWrapper(cache); }
public void Dispose() { CacheSnapshotPool.Instance.DiposeSnapshot(_pointer.Id, _cache); //Disposes the snapshot from pool for this particular pointer _cache = null; _pointer = null; _snapshot = null; }
void IEvictionPolicy.Execute(CacheBase cache, CacheRuntimeContext context, long evictSize) { ILogger NCacheLog = cache.Context.NCacheLog; if (NCacheLog.IsInfoEnabled) NCacheLog.Info("LocalCache.Evict()", "Cache Size: {0}" + cache.Count.ToString()); //if user has updated the values in configuration file then new values will be reloaded. _sleepInterval = Convert.ToInt32(ServiceConfiguration.EvictionBulkRemoveDelay); _removeThreshhold = Convert.ToInt32(ServiceConfiguration.EvictionBulkRemoveSize); DateTime startTime = DateTime.Now; ArrayList selectedKeys = this.GetSelectedKeys(cache, (long)Math.Ceiling(evictSize * _ratio)); DateTime endTime = DateTime.Now; if (NCacheLog.IsInfoEnabled) NCacheLog.Info("LocalCache.Evict()", String.Format("Time Span for {0} Items: " + (endTime - startTime), selectedKeys.Count)); startTime = DateTime.Now; Cache rootCache = context.CacheRoot; ArrayList keysTobeRemoved = new ArrayList(); ArrayList dependentItems = new ArrayList(); ArrayList removedItems = null; IEnumerator e = selectedKeys.GetEnumerator(); int removedThreshhold = _removeThreshhold/300; int remIteration = 0; while (e.MoveNext()) { object key = e.Current; if (key == null) continue; keysTobeRemoved.Add(key); if (keysTobeRemoved.Count % 300 == 0) { try { OperationContext priorityEvictionOperationContext = new OperationContext(); priorityEvictionOperationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); removedItems = cache.RemoveSync(keysTobeRemoved.ToArray(), ItemRemoveReason.Underused, false, priorityEvictionOperationContext) as ArrayList; context.PerfStatsColl.IncrementEvictPerSecStatsBy(keysTobeRemoved.Count); } catch (Exception ex) { NCacheLog.Error("PriorityEvictionPolicy.Execute", "an error occured while removing items. Error " + ex.ToString()); } keysTobeRemoved.Clear(); if (removedItems != null && removedItems.Count > 0) { dependentItems.AddRange(removedItems); } remIteration++; if (remIteration >= removedThreshhold) { //put some delay so that user operations are not affected. System.Threading.Thread.Sleep(_sleepInterval*1000); remIteration = 0; } } } if (keysTobeRemoved.Count > 0) { try { OperationContext priorityEvictionOperationContext = new OperationContext(); priorityEvictionOperationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); removedItems = cache.RemoveSync(keysTobeRemoved.ToArray(), ItemRemoveReason.Underused, false, priorityEvictionOperationContext) as ArrayList; context.PerfStatsColl.IncrementEvictPerSecStatsBy(keysTobeRemoved.Count); if (removedItems != null && removedItems.Count > 0) { dependentItems.AddRange(removedItems); } } catch (Exception ex) { NCacheLog.Error("PriorityEvictionPolicy.Execute", "an error occured while removing items. Error " + ex.ToString()); } } if (dependentItems.Count > 0) { ArrayList removableList = new ArrayList(); if (rootCache != null) { foreach (object depenentItme in dependentItems) { if (depenentItme == null) continue; removableList.Add(depenentItme); if (removableList.Count % 100 == 0) { try { OperationContext priorityEvictionOperationContext = new OperationContext(); priorityEvictionOperationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); rootCache.CascadedRemove(removableList.ToArray(), ItemRemoveReason.Underused, true, priorityEvictionOperationContext); context.PerfStatsColl.IncrementEvictPerSecStatsBy(removableList.Count); } catch (Exception exc) { NCacheLog.Error("PriorityEvictionPolicy.Execute", "an error occured while removing dependent items. Error " + exc.ToString()); } removableList.Clear(); } } if (removableList.Count > 0) { try { OperationContext priorityEvictionOperationContext = new OperationContext(); priorityEvictionOperationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); rootCache.CascadedRemove(removableList.ToArray(), ItemRemoveReason.Underused, true, priorityEvictionOperationContext); context.PerfStatsColl.IncrementEvictPerSecStatsBy(removableList.Count); } catch (Exception exc) { NCacheLog.Error("PriorityEvictionPolicy.Execute", "an error occured while removing dependent items. Error " + exc.ToString()); } removableList.Clear(); } } } return; }
/// <summary> /// /// </summary> /// <param name="cache"></param> /// <param name="evictSize"></param> /// <returns></returns> private ArrayList GetSelectedKeys(CacheBase cache, long evictSize) { ArrayList selectedKeys = new ArrayList(100); long sizeCount = 0; int prvsSize = 0; object key = null; bool selectionComplete = false; lock (_index.SyncRoot) { for (int i = 0; i < 5; i++) { if (!selectionComplete) { Hashtable currentIndex = this._index[i]; if (currentIndex != null) { IDictionaryEnumerator ide = currentIndex.GetEnumerator(); while (ide.MoveNext()) { key = ide.Key; if (key != null) { int itemSize = cache.GetItemSize(key); if (sizeCount + itemSize >= evictSize && sizeCount > 0) { if (evictSize - sizeCount > (itemSize + sizeCount) - evictSize) selectedKeys.Add(key); selectionComplete = true; break; } else { selectedKeys.Add(key); sizeCount += itemSize; prvsSize = itemSize; } } } } } else { //break the outer loop. we have already picked up //the keys to be evicited. break; } } } return selectedKeys; }
/// <summary> /// Returns an array containing list of keys contained in the cache. Null if there are no /// keys or if timeout occurs. /// </summary> /// <param name="cache"></param> /// <param name="timeout"></param> /// <returns></returns> internal static object[] GetKeyset(CacheBase cache, int timeout) { ulong index = 0; object[] objects = null; cache.Sync.AcquireWriterLock(timeout); try { if (!cache.Sync.IsWriterLockHeld || cache.Count < 1) return objects; objects = new object[cache.Count]; for (IEnumerator i = cache.GetEnumerator(); i.MoveNext(); ) { objects[index++] = ((DictionaryEntry)i.Current).Key; } } finally { cache.Sync.ReleaseWriterLock(); } return objects; }