using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Reflection;
namespace DataAccess
{
public static class CacheHelper
{
private static ICacheManager cache = CacheFactory.GetCacheManager();
/// <summary>
/// Add Cache
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="isRefresh">if true, should reload data every 5 mins; default value = false</param>
public static void Add(string key, object value, bool isRefresh)
{
if (isRefresh)
cache.Add(key, value, CacheItemPriority.Normal, new ATSCacheItemRefreshAction(), new AbsoluteTime(TimeSpan.FromMinutes(5)));
else
cache.Add(key, value);
}
// Summary:
// Adds new CacheItem to cache. If another item already exists with the same
// key, that item is removed before the new item is added. If any failure occurs
// during this process, the cache will not contain the item being added. Items
// added with this method will be not expire, and will have a Normal Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority
// priority.
//
// Parameters:
// key:
// Identifier for this CacheItem
//
// value:
// Value to be stored in cache. May be null.
//
// Exceptions:
// System.ArgumentNullException:
// Provided key is null
//
// System.ArgumentException:
// Provided key is an empty string
//
// Remarks:
// The CacheManager can be configured to use different storage mechanisms in
// which to store the CacheItems. Each of these storage mechanisms can throw
// exceptions particular to their own implementations.
public static void Add(string key, object value)
{
Add(key, value, false);
}
public static object Get(string key)
{
return cache.GetData(key);
}
public static void Remove(string key)
{
cache.Remove(key);
}
}
[Serializable]
public class ATSCacheItemRefreshAction : ICacheItemRefreshAction
{
#region ICacheItemRefreshAction Members
public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
{
//when expired, reload it.
if (removalReason == CacheItemRemovedReason.Expired)
{
ICacheManager c = CacheFactory.GetCacheManager();
c.Add(removedKey, expiredValue);
}
}
#endregion
}