每周200分,长期专栏,体验并推广ASP向ASP.NET过渡。之四:缓存
超级大笨狼 2005-06-07 12:19:24 这次讨论一下缓存,个人认为:没有缓存机制,面向对象大打折扣。
一段缓存的例子:
public class CacheUtil
{
public static Topic GetTopic(int topicid)
{
string key="__Forum_Topic__"+topicid;
Topic topic=null;
if(HttpContext.Current.Cache[key]!=null)
{
topic=(Topic)HttpContext.Current.Cache[key];
//HttpContext.Current.Response.Write("retrieve topic "+topicid+" from cache<br>");
}
else
{
//HttpContext.Current.Response.Write("retrieve topic "+topicid+" from database<br>");
topic=Topic.GetTopic(topicid);
HttpContext.Current.Cache.Add(key,topic,null,DateTime.MaxValue,new TimeSpan(0,0,15,0),CacheItemPriority.High,null);
}
return topic;
}
略。。。。。。