不使用数据库缓存依赖项实现同样的功能[转]
2013-08-07 17:47
549 查看
不使用数据库缓存依赖项实现同样的功能
先说下当前项目的场景 后台人员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";
相关文章推荐
- 不使用数据库缓存依赖项实现同样的功能
- 基于 自定义注解 和 aop 实现使用memcache 对数据库的缓存 示例
- 基于nginx实现缓存功能及uptream模块详细使用方法
- 使用poi实现Excel导入功能,数据库基于oracle(上)
- .NET 数据库缓存依赖策略实现
- 使用shareperference实现缓存cache功能
- 装饰者模式---使用装饰者模式实现带日志记录功能的数据库命令执行类
- 使用silverlight构建一个工作流设计器(十八)-持久化数据到数据库—服务器段功能实现
- IONIC 中使用拦截功能来实现API接口访问的数据缓存
- ASP.NET2.0结合aspnet_regsql实现数据库的缓存依赖
- 能够正常使用的,经过测试的java+jquery+webcam调用本地摄像头拍照,然后将拍照结果上传数据库blob字段功能的实现(二)
- JAVA中使用redis+protoStuff实现数据库缓存机制
- asp.net 实现文件缓存依赖,数据库缓存依赖
- ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效
- IONIC 中使用拦截功能来实现API接口访问的数据缓存
- 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能
- ASP.NET MVC 数据库依赖缓存的实现
- 技巧和诀窍:使用ASP.NET 2.0 输出缓存替换的功能实现“甜圈缓存(Donut Caching)”
- 在 Laravel 中使用 Laravel Searchy 扩展包实现基于数据库的轻量级搜索功能
- 缓存依赖、数据库缓存依赖学习以及在LinQ当中使用缓存依赖