不使用数据库缓存依赖项实现同样的功能[转]

不使用数据库缓存依赖项实现同样的功能

先说下当前项目的场景 后台人员1天不定时的 添加新闻和视频图片10条左右 数量不多

不使用缓存  来回和数据库打交道 非常耗时 造成效率低  尤其是在数据量非常庞大的情况下

可是加了缓存 加多少时间的缓存? 新闻要保证实时更新 一发布立刻显示出来

微软给出了解决方法 数据库缓存依赖项 但是貌似只能用在SQL上 而且要配置些东西  还有 并不透明~ 一些东西看不到

http://msdn.microsoft.com/zh-cn/library/system.web.caching.sqlcachedependency%28VS.80%29.aspx 

感兴趣的朋友可以查下MSDN 这有个博客 也有介绍

http://www.cnblogs.com/ltp/archive/2009/06/30/1514331.html

这里提供另一种方法 

先说下大概思路  在所有查找操作时 都把结果插入 cache  在对数据库有操作时(增删改)  删除cache

有了思路 开始解决问题 这里会遇到两个问题

第一 :  所有查找时  cache建 怎么定义 保证不重复呢   第二:微软没有提供删除所有cache的方法 只有cache["键值名"].Remove();  而没有removeall();

下面上代码和解决办法 以及思路

复制代码
#region##根据条件得到新闻
///<summary>
/// 根据条件得到新闻
///</summary>
///<param name="tableName"></param>
///<param name="whereStr"></param>
///<param name="topCount"></param>
///<returns></returns>
public List<CmsDemoModels.NewsInfo> GetByCondition(string whereStr, string topCount)
{
string cacheKey =string.Format("{0}.{1}.{2}", GetType(), "GetByCondition",whereStr+topCount);
if (HttpRuntime.Cache[cacheKey] !=null)
{
return HttpRuntime.Cache[cacheKey] as List<CmsDemoModels.NewsInfo>;
}
else
{
//从数据库里查找并插入缓存
using (CmsDemoDAL.NewsInfoServcie ns =new NewsInfoServcie())
{
List
<NewsInfo> newsList=ns.GetByCondition(whereStr, topCount);
HttpRuntime.Cache.Insert(cacheKey,newsList,
null, DateTime.Now.AddDays(1), TimeSpan.Zero);
return newsList;
}
}
}
复制代码
看上面的代码
string cacheKey = string.Format("{0}.{1}.{2}", GetType(), "GetByCondition",whereStr+topCount);
我定义这缓存键值不重复的方法是 用当前类+方法名+所有参数名的组合 来保证唯一性

这样把所有查询的方法 以及查询结果都缓存起来了~

复制代码
publicstaticvoid ClearOutputCache()
{
//移除自定义缓存
foreach (var item in HttpRuntime.Cache.Cast<DictionaryEntry>().ToArray())
{
HttpRuntime.Cache.Remove((
string)item.Key);
}
复制代码
上面的方法 是删除所有缓存

可是又有个问题 我们有视频表 图片表 新闻 等等 我现在更新个新闻 就要删除所有的缓存 其实只用删除所有新闻的缓存就行了

复制代码
#region 删除缓存

///<summary>
/// 根据名字开头删除缓存
///</summary>
///<param name="StartName">缓存名字开头</param>
publicvoid RemoveAllCache(string StartName)
{
//移除自定义应用程序缓存
DictionaryEntry[] de = HttpRuntime.Cache.Cast<DictionaryEntry>().ToArray();
foreach (var item in de)
{
string cacheKey = item.Key.ToString();
if (cacheKey.StartsWith(StartName))
{
HttpRuntime.Cache.Remove((
string)item.Key);
}
}
}

#endregion
复制代码
稍微改进下 效率又大大的提高了

当我们数据库有变化时 比如添加了个新闻 调用
p.RemoveAllCache(GetType().ToString());
复制代码
#region##添加新闻
///<summary>
/// 添加新闻
///</summary>
///<param name="info"></param>
///<returns></returns>
publicint Add(CmsDemoModels.NewsInfo info)
{

using (CmsDemoDAL.NewsInfoServcie ns =new NewsInfoServcie())
{
info.ViewCount
=0;
info.State
=0;
info.SortIndex
= GetMaxSort() +1;
int i= ns.Add(info);
PubClass p
=new PubClass();
p.RemoveAllCache(GetType().ToString());
return i;
}
}
#endregion
复制代码

 这样就把所有以GetType().ToString() 开头的删除掉了~~ 实现新闻的删除新闻的  视频的删除视频的 

PS: 这里新闻添加和 查找 都是在BLL层下的 NewInfoManager类下 所以他们的 GetType().ToString() 会一样

大概思路就这样

有什么问题 可以留言交流 欢迎讨论~

discunz.net

缓存健的设置方法:

/TOPIC_LIST
public const string FORUM_TOPIC_LIST_FID = "/Forum/TopicList/{0}";

public const string FORUM_ADMIN_GROUP_LIST = "/Forum/AdminGroupList";
public const string FORUM_USER_GROUP_LIST = "/Forum/UserGroupList";
public const string FORUM_MODERATOR_LIST = "/Forum/ModeratorList";
public const string FORUM_ANNOUNCEMENT_LIST = "/Forum/AnnouncementList";
public const string FORUM_SIMPLIFIED_ANNOUNCEMENT_LIST = "/Forum/SimplifiedAnnouncementList";
public const string FORUM_SETTING = "/Forum/Setting";
public const string FORUM_VALID_SCORE_NAME = "/Forum/ValidScoreName";
public const string FORUM_VALID_SCORE_UNIT = "/Forum/ValidScoreUnit";
public const string FORUM_URLS = "/Forum/Urls";
public const string FORUM_STATISTICS = "/Forum/Statistics";
public const string FORUM_ONLINE_ICON_TABLE = "/Forum/OnlineIconTable";
public const string FORUM_FORUM_LINK_LIST = "/Forum/ForumLinkList";
public const string FORUM_BAN_WORD_LIST = "/Forum/BanWordList";
public const string FORUM_FORUM_LIST = "/Forum/ForumList";
public const string FORUM_TEMPLATE_ID_LIST = "/Forum/TemplateIDList";
public const string FORUM_LAST_POST_TABLE_NAME = "/Forum/LastPostTableName";
public const string FORUM_POST_TABLE_NAME = "/Forum/PostTableName";
public const string FORUM_ADVERTISEMENTS = "/Forum/Advertisements";
public const string FORUM_STATISTICS_SEARCHTIME = "/Forum/StatisticsSearchtime";
public const string FORUM_STATISTICS_SEARCHCOUNT = "/Forum/StatisticsSearchcount";
public const string FORUM_COMMON_AVATAR_LIST = "/Forum/CommonAvatarList";
public const string FORUM_MAGIC_LIST = "/Forum/MagicList";
public const string FORUM_SCORE_PAY_SET = "/Forum/ScorePaySet";
public const string FORUM_TOPIC_LIST_FORMAT = "/Forum/TopicList-{0}-{1}-{2}-{3}-{4}-{5}";
public const string SPACE_ALBUM_CATEGORY = "/Space/AlbumCategory";
public const string FORUM_FORUM_LIST_MENU_DIV = "/Forum/ForumListMenuDiv";

原文地址:https://www.cnblogs.com/fx2008/p/3243637.html