using System;
using System.Web ;
using System.Web.Caching;
using System.Configuration ;
using System.Collections ;
namespace Jawad.Utils.CacheManagement
{
///
/// Summary description for DataCache.
///
sealed public class HttpCache
{
///
/// Singleton instance of Data Cache Class
///
public static readonly HttpCache httpCache = new HttpCache();
private Cache _cache;
private int _cacheDuration = 30 ; // Default value is 30 minutes ....
private HttpCache()
{
_cache = System.Web.HttpContext.Current.Cache;
string duration = ConfigurationSettings.AppSettings["CacheDurationInMinutes"];
if ( duration != null)
_cacheDuration = int.Parse (duration);
}
///
/// Get the Item from the Cache ...
///
/// This is the key to look for when accessing Cache Item
/// Data to be stored in the Cache
/// True if Key is found in the Cache ...
public bool GetItem(string key, out object item)
{
bool result = false;
item = _cache[key];
if ( item != null )
result = true;
return result;
}
///
/// Set the Item in the Cache ....
///
/// The is the Key to be used to access the data Later ...
/// This is the actual data to be stored in Cache ...
public void SetItem(string key, object item)
{
_cache.Insert(key, item, null,
DateTime.Now.AddMinutes(_cacheDuration), TimeSpan.Zero);
}
///
/// Set the Item in the Cache that expires after the specified duration in Minutes ....
///
/// The is the Key to be used to access the data Later ...
/// This is the actual data to be stored in Cache ...
/// Minutes to keep the data in Cache ...
public void SetItemForDuration(string key, object item, double MinutesDuration)
{
_cache.Insert(key, item, null,
DateTime.Now.AddMinutes(MinutesDuration), TimeSpan.Zero);
}
///
/// Set the Item in the Cache that expires if not accessed for certain duration ....
///
/// The is the Key to be used to access the data Later ...
/// This is the actual data to be stored in Cache ...
/// Minutes till last accessed to clear the Cache ...
public void SetItemClearIfNotAccessed(string key, object item, double MinutesDuration)
{
_cache.Insert(key, item, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(MinutesDuration));
}
///
/// Set the Item in the Cache that will never expire ....
///
/// This is the Key to be used to access the data Later ...
/// This is the actual data to be stored in Cache ...
public void SetItemNeverExpire(string key, object item)
{
_cache.Insert(key, item, null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
}
///
/// Set the Item in the Cache that will Refresh it self by calling provided function ....
///
/// This is the Key to be used to access the data Later ...
/// This is the actual data to be stored in Cache ...
/// Duration in Minutes to keep the item in Cache before refresh is called. Default Duration can Be acquired from Web.Config AppSettings
/// A Function with the following Signature
/// public void CdiCacheItemRemovedCallback(string key, object val, CacheItemRemovedReason reason)
/// inside this fuction you should get the data again and then call "SetItemRefreshItSelf" function passing it
/// same function name you are in i.e. CdiCacheItemRemovedCallBack as CdiCacheItemRemovedCallback parameter
///
public void SetItemRefreshItSelf(string key, object item,
int durationMinutes, CacheItemRemovedCallback CdiCacheItemRemovedCallback)
{
//create an instance of the callback delegate
CacheItemRemovedCallback callBack =
new CacheItemRemovedCallback(CdiCacheItemRemovedCallback);
//insert the item in cache that expires in 10 seconds.
//assign the callback instance to it
_cache.Insert(key, item, null,
DateTime.Now.AddMinutes(durationMinutes), Cache.NoSlidingExpiration,
CacheItemPriority.Default, callBack);
}
// SAMPLE DELEGATE FOR CACHE REFRESH
// public void CdiCacheItemRemovedCallback(string key,
// object val, CacheItemRemovedReason reason)
// {
// //set cache status, so that we can view what
// //happened the next time we request a status update
// Cache.Insert("CacheStatus","KEY: " + key + "
VALUE: "
// + val + "
REASON: " + reason.ToString());
// }
///
/// Remove all the Cache Items from the Current Cache ...
///
public string RemoveAllCache()
{
string cacheItemsCleared = "Cache cleared for :
";
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
string key = CacheEnum.Key.ToString();
HttpRuntime.Cache.Remove(key);
cacheItemsCleared += key + "
";
}
if (cacheItemsCleared.Length < 32)
cacheItemsCleared = "No items in Cache to clear.";
return cacheItemsCleared;
}
///
/// Removes the desired object from the Cache
///
/// The key for the Cache object to be removed
public void RemoveParticularObject(string key)
{
object garbage = _cache.Remove (key);
}
}
}