private void CreateCache(string name, long cacheMemoryLimitMegabytes, long physicalMemoryLimitPercentage, TimeSpan pollingInterval) { var nameValueCollection = new NameValueCollection(); nameValueCollection.Add("cacheMemoryLimitMegabytes", cacheMemoryLimitMegabytes.ToString()); nameValueCollection.Add("physicalMemoryLimitPercentage", physicalMemoryLimitPercentage.ToString()); nameValueCollection.Add("pollingInterval", pollingInterval.ToString()); memoryCache_ = new EnumerableMemoryCache(name, nameValueCollection); }
public void Clear() { lock (SyncRoot) { memoryCache_.Dispose(); memoryCache_ = null; CreateCache(name_, cacheMemoryLimitMegabytes_, physicalMemoryLimitPercentage_, pollingInterval_); } }
protected override void Dispose(bool disposing) { Log.Info(string.Format("'{0}:{1}' Dispose()", GetType().FullName, GetHashCode())); if (disposing) { cacheItemPolicy_.RemovedCallback = null; memoryCache_.Dispose(); memoryCache_ = null; } }
public void Remove(string key, string regionName = null) { lock (SyncRoot) { if (!Contains(EnumerableMemoryCache.BuildKey(key, regionName))) { return; } memoryCache_.Remove(EnumerableMemoryCache.BuildKey(key, regionName)); } }
public void AddRegion(string regionName, IEnumerable <KeyValuePair <string, object> > region, bool unlockIt = true) { lock (SyncRoot) { RemoveRegion(regionName); foreach (var item in region) { memoryCache_.Add(new CacheItem(EnumerableMemoryCache.BuildKey(item.Key, null), new LockableValue(item.Value, locked: false)), cacheItemPolicy_); } } }
public void Add(string key, object value, bool unLockit = true, string regionName = null) { lock (SyncRoot) { var item = memoryCache_.Get(EnumerableMemoryCache.BuildKey(key, regionName)) as LockableValue; if (item == null) { memoryCache_.Add(new CacheItem(EnumerableMemoryCache.BuildKey(key, regionName), new LockableValue(value, locked: false)), cacheItemPolicy_); return; } item.Value = value; item.Locked = !unLockit && item.Locked; } }
public object Get(string key, bool lockIt = false, string regionName = null) { lock (SyncRoot) { var value = memoryCache_.Get(EnumerableMemoryCache.BuildKey(key, regionName)) as LockableValue; if (value == null) { return(null); } if (value.Locked) { throw new ObjectLockedException(string.Format("Item with key '{0}' is locked", key)); } if (lockIt) { Add(key, value.Value, unLockit: false, regionName: regionName); } return(value.Value); } }