如果网站浏览量特别大,而且服务器配置又非常低怎么办?基本上所有回答都会说使用缓存。
那么什么情况下需要使用缓存呢?
1. 单个对象,例如一篇新闻,一个在短时间内不会被更新的列表
2. 会经常调配的配置等
以下为一个列表的案例,配置参数由Dictionary<string,object>传递
string t = "";
if (dic1 != null)
{
foreach (var item in dic1)
{
t += "-" + item.Value;
}
}
if (dic2 != null)
{
foreach (var item in dic2)
{
t += "-" + item.Value;
}
}
string key = DataCacheKey.TOUR.ToString() + "_" + pageindex + "_" + pagesize + t;
string countkey = key + "_COUNT";
List<CMS_Tour> list = (List<CMS_Tour>)DataCache.GetCache(key);
total = DataCache.GetCache(countkey) == null ? -1 : (int)DataCache.GetCache(countkey);
if (list == null)
{
list = CMS_TourBaseDAL.GetQueryList(dic1, dic2, "Tid", pageindex, pagesize, out total);
DataCache.SetCache(key, list);
DataCache.SetCache(countkey, total);
}
if (total < 0)
{
DataCache.SetCache(countkey, 0);
}
return list;
特别说明:
1. 在对象增删改之类的时候,依然对缓存进行改动,然后用SQL更改对象属性,例如点击率之类的
2. 缓存使用范围一定要慎重,如果全部都缓存,然而在某些需要被搜索引擎抓取的内容长期抓不到,那不是很不好么?
3. 缓存工具在我们的第四节课有附件可以使用:铜梁视窗代码生成器
感谢各位大牛来围观本文,也来支持小弟的小站吧--》铜梁视窗