
代码:
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
namespace Ycweb.Components

{
public class SiteCache
{
private static readonly Cache _cache;
public static readonly int DayFactor;
private static int Factor;
public static readonly int HourFactor;
public static readonly int MinuteFactor;
static SiteCache()
{
DayFactor = 17280;
HourFactor = 720;
MinuteFactor = 12;
Factor = 5;
_cache = HttpRuntime.Cache;
}
private SiteCache()
{
}
public static void Clear()
{
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
while (enumerator.MoveNext())
{
_cache.Remove(enumerator.Key.ToString());
}
}
public static object Get(string key)
{
return _cache[key];
}
public static void Insert(string key, object obj)
{
Insert(key, obj, null, 1);
}
public static void Insert(string key, object obj, int seconds)
{
Insert(key, obj, null, seconds);
}
public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, HourFactor*12);
}
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
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((double) (Factor*seconds)), TimeSpan.Zero, priority, null);
}
}
public static void Max(string key, object obj)
{
Max(key, obj, null);
}
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);
}
}
public static void MicroInsert(string key, object obj, int secondFactor)
{
if (obj != null)
{
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor*secondFactor)), TimeSpan.Zero);
}
}
public static void Remove(string key)
{
_cache.Remove(key);
}
public static void RemoveByPattern(string pattern)
{
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
Regex regex1 = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
while (enumerator.MoveNext())
{
if (regex1.IsMatch(enumerator.Key.ToString()))
{
_cache.Remove(enumerator.Key.ToString());
}
}
}
public static void ReSetFactor(int cacheFactor)
{
Factor = cacheFactor;
}


}
}
有了SiteCache类,接下来看看如何使用它。还是以读取新闻TonN列表为例:
public static RecordSet GetNewsSetTopN(string classCode,int topN,SortPostsBy orderBy, SortOrder sortOrder, string language)

{
string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}:OB:{3}:SO:{4}", language,classCode,topN.ToString(), orderBy.ToString(),sortOrder.ToString());
//从上下文中读缓存项
RecordSet newsSet = HttpContext.Current.Items[cacheKey] as RecordSet;
if (newsSet == null)
{
//从HttpRuntime.Cache读缓存项
newsSet = SiteCache.Get(cacheKey) as RecordSet;
if (newsSet == null)
{
//直接从数据库从读取
CommonDataProvider dp=CommonDataProvider.Instance();
newsSet =dp.GetNewsSetTopN(language,classCode,topN,orderBy,sortOrder);
//并将结果缓存到HttpRuntime.Cache中
SiteCache.Insert(cacheKey, newsSet, 60, CacheItemPriority.Normal);
}
}
return newsSet;
}

/**//// <summary>
/// 删除匹配的NewsSetTopN列表的Cache项
/// </summary>
public static void ClearNewsSetTopNCache(string language,string classCode,int topN)

{
string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}",language,classCode,topN.ToString());
SiteCache.RemoveByPattern(cacheKey);
}
发布新闻后调用静态方法ClearNewsSetTopNCache()强行清除原来的TopN缓存项,例如:

/**//// <summary>
/// 发布(新建)新闻
/// </summary>
/// <param name="post">新闻实例</param>
/// <returns>返回状态</returns>
public static int Create(News post)

{
int status;
CommonDataProvider dp=CommonDataProvider.Instance();
dp.CreateUpdateDeleteNews(post, DataAction.Create, out status);
//强制清除匹配的缓存项
ClearNewsSetTopNCache (post.Language, post.ClassCode,Globals.GetSiteSetting.NewsListTopN);
return status;
}
That"s all.若有不妥之处还望各位同行指正。