1. 时间点(指定时间点内有效);
2. KEY值(KEY值作Cache项标识);
3. 文件或目录(指定文件或目录变更,则原Cache项不可用);
代码:
1
using System;2
using System.Collections;3
using System.Text.RegularExpressions;4
using System.Web;5
using System.Web.Caching;6

7
namespace Ycweb.Components8
{9
public class SiteCache10
{11
private static readonly Cache _cache;12
public static readonly int DayFactor;13
private static int Factor;14
public static readonly int HourFactor;15
public static readonly int MinuteFactor;16

17
static SiteCache()18
{19
DayFactor = 17280;20
HourFactor = 720;21
MinuteFactor = 12;22
Factor = 5;23
_cache = HttpRuntime.Cache;24
}25

26
private SiteCache()27
{28
}29

30
public static void Clear()31
{32
IDictionaryEnumerator enumerator = _cache.GetEnumerator();33
while (enumerator.MoveNext())34
{35
_cache.Remove(enumerator.Key.ToString());36
}37
}38

39
public static object Get(string key)40
{41
return _cache[key];42
}43

44
public static void Insert(string key, object obj)45
{46
Insert(key, obj, null, 1);47
}48

49
public static void Insert(string key, object obj, int seconds)50
{51
Insert(key, obj, null, seconds);52
}53

54
public static void Insert(string key, object obj, CacheDependency dep)55
{56
Insert(key, obj, dep, HourFactor*12);57
}58

59
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)60
{61
Insert(key, obj, null, seconds, priority);62
}63

64
public static void Insert(string key, object obj, CacheDependency dep, int seconds)65
{66
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);67
}68

69
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)70
{71
if (obj != null)72
{73
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double) (Factor*seconds)), TimeSpan.Zero, priority, null);74
}75
}76

77
public static void Max(string key, object obj)78
{79
Max(key, obj, null);80
}81

82
public static void Max(string key, object obj, CacheDependency dep)83
{84
if (obj != null)85
{86
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);87
}88
}89

90
public static void MicroInsert(string key, object obj, int secondFactor)91
{92
if (obj != null)93
{94
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor*secondFactor)), TimeSpan.Zero);95
}96
}97

98
public static void Remove(string key)99
{100
_cache.Remove(key);101
}102

103
public static void RemoveByPattern(string pattern)104
{105
IDictionaryEnumerator enumerator = _cache.GetEnumerator();106
Regex regex1 = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);107
while (enumerator.MoveNext())108
{109
if (regex1.IsMatch(enumerator.Key.ToString()))110
{111
_cache.Remove(enumerator.Key.ToString());112
}113
}114
}115

116
public static void ReSetFactor(int cacheFactor)117
{118
Factor = cacheFactor;119
}120

121

122

123
}124
}
有了SiteCache类,接下来看看如何使用它。还是以读取新闻TonN列表为例:
public static RecordSet GetNewsSetTopN(string classCode,int topN,SortPostsBy orderBy, SortOrder sortOrder, string language)2
{3
string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}:OB:{3}:SO:{4}", language,classCode,topN.ToString(), orderBy.ToString(),sortOrder.ToString());4
5
//从上下文中读缓存项6
RecordSet newsSet = HttpContext.Current.Items[cacheKey] as RecordSet;7
if (newsSet == null)8
{9
//从HttpRuntime.Cache读缓存项10
newsSet = SiteCache.Get(cacheKey) as RecordSet;11
if (newsSet == null)12
{13
//直接从数据库从读取14
CommonDataProvider dp=CommonDataProvider.Instance();15
newsSet =dp.GetNewsSetTopN(language,classCode,topN,orderBy,sortOrder);16
//并将结果缓存到HttpRuntime.Cache中17
SiteCache.Insert(cacheKey, newsSet, 60, CacheItemPriority.Normal);18
}19
20
}21
return newsSet;22
}
/// <summary>2
/// 删除匹配的NewsSetTopN列表的Cache项3
/// </summary>4
public static void ClearNewsSetTopNCache(string language,string classCode,int topN)5
{6
string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}",language,classCode,topN.ToString());7
SiteCache.RemoveByPattern(cacheKey);8
}9

发布新闻后调用静态方法ClearNewsSetTopNCache()强行清除原来的TopN缓存项,例如:
/// <summary>2
/// 发布(新建)新闻3
/// </summary>4
/// <param name="post">新闻实例</param>5
/// <returns>返回状态</returns>6
public static int Create(News post)7
{8
int status;9
CommonDataProvider dp=CommonDataProvider.Instance();10
dp.CreateUpdateDeleteNews(post, DataAction.Create, out status);11
//强制清除匹配的缓存项12
ClearNewsSetTopNCache (post.Language, post.ClassCode,Globals.GetSiteSetting.NewsListTopN);13
return status;14
}
That's all.若有不妥之处还望各位同行指正。
Mikel

