using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
namespace CropWeb.Library
{
/// <summary>
/// 緩存管理
/// </summary>
public class CacheHelper
{
private CacheHelper() { }
//>> Based on Factor = 5 default value
public static readonly int DayFactor = 17280;
public static readonly int HourFactor = 720;
public static readonly int MinuteFactor = 12;
public static readonly double SecondFactor = 0.2;
private static readonly Cache _cache;
private static int Factor = 5;
public static void ReSetFactor(int cacheFactor)
{
Factor = cacheFactor;
}
/// <summary>
/// Static initializer should ensure we only have to look up the current cache
/// instance once.
/// </summary>
static CacheHelper()
{
HttpContext context = HttpContext.Current;
if (context != null)
{
_cache = context.Cache;
}
else
{
_cache = HttpRuntime.Cache;
}
}
/// <summary>
/// 清空Cash對象
/// </summary>
public static void Clear()
{
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
}
foreach (string key in al)
{
_cache.Remove(key);
}
}
/// <summary>
/// 根據正則表達式的模式移除Cache
/// </summary>
/// <param name="pattern">模式</param>
public static void RemoveByPattern(string pattern)
{
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
while (CacheEnum.MoveNext())
{
if (regex.IsMatch(CacheEnum.Key.ToString()))
_cache.Remove(CacheEnum.Key.ToString());
}
}
/// <summary>
/// 根據鍵值移除Cache
/// </summary>
/// <param name="key">鍵</param>
public static void Remove(string key)
{
_cache.Remove(key);
}
/// <summary>
/// 把對象加載到Cache
/// </summary>
/// <param name="key">鍵</param>
/// <param name="obj">對象</param>
public static void Insert(string key, object obj)
{
Insert(key, obj, null, 1);
}
/// <summary>
/// 把對象加載到Cache,附加緩存依賴信息
/// </summary>
/// <param name="key">鍵</param>
/// <param name="obj">對象</param>
/// <param name="dep">緩存依賴</param>
public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, MinuteFactor * 3);
}
/// <summary>
/// 把對象加載到Cache,附加過時時間信息
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="seconds">緩存時間(秒)</param>
public static void Insert(string key, object obj, int seconds)
{
Insert(key, obj, null, seconds);
}
/// <summary>
/// 把對象加載到Cache,附加過時時間信息和優先級
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="seconds">緩存時間(秒)</param>
/// <param name="priority">優先級</param>
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
/// <summary>
/// 把對象加載到Cache,附加緩存依賴和過時時間(多少秒後過時)
/// (默認優先級爲Normal)
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="dep">緩存依賴</param>
/// <param name="seconds">緩存時間(秒)</param>
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
/// <summary>
/// 把對象加載到Cache,附加緩存依賴和過時時間(多少秒後過時)及優先級
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="dep">緩存依賴</param>
/// <param name="seconds">緩存時間(秒)</param>
/// <param name="priority">優先級</param>
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
}
}
/// <summary>
/// 把對象加到緩存並忽略優先級
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="secondFactor">時間</param>
public static void MicroInsert(string key, object obj, int secondFactor)
{
if (obj != null)
{
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
}
}
/// <summary>
/// 把對象加到緩存,並把過時時間設爲最大值
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
public static void Max(string key, object obj)
{
Max(key, obj, null);
}
/// <summary>
/// 把對象加到緩存,並把過時時間設爲最大值,附加緩存依賴信息
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="dep">緩存依賴</param>
public static void Max(string key, object obj, CacheDependency dep)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
}
}
/// <summary>
/// 插入持久性緩存
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
public static void Permanent(string key, object obj)
{
Permanent(key, obj, null);
}
/// <summary>
/// 插入持久性緩存,附加緩存依賴
/// </summary>
/// <param name="key">鍵值</param>
/// <param name="obj">對象</param>
/// <param name="dep">緩存依賴</param>
public static void Permanent(string key, object obj, CacheDependency dep)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
}
}
/// <summary>
/// 根據鍵獲取被緩存的對象
/// </summary>
/// <param name="key">鍵值</param>
/// <returns></returns>
public static object Get(string key)
{
return _cache[key];
}
/// <summary>
/// Return int of seconds * SecondFactor
/// </summary>
public static int SecondFactorCalculate(int seconds)
{
// Insert method below takes integer seconds, so we have to round any fractional values
return Convert.ToInt32(Math.Round((double)seconds * SecondFactor));
}
}
}正則表達式