/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections; using System.Collections.Generic; using System.Web; using System.Web.Caching; namespace GreenshotPlugin.Core { public delegate void CacheObjectExpired(string key, object cacheValue); /// /// Description of CacheHelper. /// public class CacheHelper { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger("CacheHelper"); private Cache cache = HttpRuntime.Cache; private string prefix; private double defaultExpiration = 10*60; // 10 Minutes private CacheItemRemovedCallback defaultCallback = null; private CacheObjectExpired expiredCallback = null; public CacheHelper(string prefix) { defaultCallback = new CacheItemRemovedCallback(OnRemoved); this.prefix = prefix + "."; } public CacheHelper(string prefix, double defaultExpiration) : this(prefix) { this.defaultExpiration = defaultExpiration; } public CacheHelper(string prefix, double defaultExpiration, CacheObjectExpired expiredCallback) : this(prefix, defaultExpiration) { this.expiredCallback = expiredCallback; } private void OnRemoved(string key, object cacheValue, CacheItemRemovedReason reason) { LOG.DebugFormat("The item with key '{0}' is being removed from the cache with reason: {1}", key, reason); switch (reason) { case CacheItemRemovedReason.Expired: if (expiredCallback != null) { expiredCallback.Invoke(key, cacheValue); } break; case CacheItemRemovedReason.Underused: break; } } /// /// Insert value into the cache using default expiration & default callback /// /// Item to be cached /// Name of item public void Add(string key, T o) { if (defaultCallback != null) { cache.Insert(prefix + key, o, null, DateTime.Now.AddSeconds(defaultExpiration), Cache.NoSlidingExpiration, CacheItemPriority.Default, defaultCallback); } else { cache.Insert(prefix + key, o, null, DateTime.Now.AddSeconds(defaultExpiration), Cache.NoSlidingExpiration); } } /// /// Get all the methods for this cache /// /// IEnumerator of the type public IEnumerable GetElements() { IDictionaryEnumerator cacheEnum = cache.GetEnumerator(); while (cacheEnum.MoveNext()) { string key = cacheEnum.Key as string; if (!string.IsNullOrEmpty(key) && key.StartsWith(prefix)) { yield return (T)cacheEnum.Value; } } } /// /// Insert value into the cache using the supplied expiration time in seconds /// /// Item to be cached /// Name of item /// expiration time in "double" seconds public void Add(string key, T o, double seconds) { cache.Insert(prefix + key, o, null, DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration); } /// /// Insert value into the cache using /// appropriate name/value pairs /// /// Item to be cached /// Name of item public void Add(string key, T o, CacheItemRemovedCallback callback) { cache.Insert(prefix + key, o, null, DateTime.Now.AddSeconds(defaultExpiration), Cache.NoSlidingExpiration, CacheItemPriority.Default, callback); } /// /// Remove item from cache /// /// Name of cached item public void Remove(string key) { cache.Remove(prefix + key); } /// /// Check for item in cache /// /// Name of cached item /// public bool Exists(string key) { return cache[prefix + key] != null; } /// /// Retrieve cached item /// /// Name of cached item /// Cached item as type public T Get(string key) { try { return (T) cache[prefix + key]; } catch { return default(T); } } } }