码迷,mamicode.com
首页 > 系统相关 > 详细

优化:Cache缓存

时间:2015-08-13 14:21:01      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 1 降低数据库压力
 2 <appSettings><add key="ModelCache" value="1"/></appSettings> //设置实体缓存时间
 3 
 4     public RupengWang.Model.Course GetModelByCache(long Id)
 5     {
 6         string CacheKey = "CourseModel-" + Id;
 7         object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
 8         if (objModel == null)
 9         {
10             objModel = dal.GetModel(Id);
11             if (objModel != null)
12             {
13                 int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
14                 Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
15             }
16         }
17         return (RupengWang.Model.Course)objModel;
18     }

2.MVC中缓存

 1 /// <summary>
 2         /// 从缓存中获得自定义匿名对象
 3         /// </summary>
 4         /// <returns></returns>
 5         public object GetModelByCache()
 6         {
 7             string CacheKey = "MyProductAndGroupModel_";
 8             object objModel = HttpRuntime.Cache[CacheKey]; //获得当前应用程序的缓存
 9             if (objModel == null)
10             {
11                 objModel = GetDefineModelByEdm();
12                 if (objModel != null)
13                 {
14                     //int ModelCache = Convert.ToInt32(ConfigurationManager.AppSettings["ModelCache"]);
15                     HttpRuntime.Cache.Insert(CacheKey, objModel, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);
16                 }
17             }
18             return objModel;
19         }

 

参考:

http://www.cnblogs.com/zgx/archive/2009/03/16/1413643.html

http://bbs.csdn.net/topics/310107146

优化:Cache缓存

标签:

原文地址:http://www.cnblogs.com/adolphyang/p/4727025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!