降低数据库压力
<appSettings><add key="ModelCache" value=""/></appSettings> //设置实体缓存时间 public RupengWang.Model.Course GetModelByCache(long Id)
{
string CacheKey = "CourseModel-" + Id;
object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
if (objModel == null)
{
objModel = dal.GetModel(Id);
if (objModel != null)
{
int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
}
}
return (RupengWang.Model.Course)objModel;
}

2.MVC中缓存

 /// <summary>
/// 从缓存中获得自定义匿名对象
/// </summary>
/// <returns></returns>
public object GetModelByCache()
{
string CacheKey = "MyProductAndGroupModel_";
object objModel = HttpRuntime.Cache[CacheKey]; //获得当前应用程序的缓存
if (objModel == null)
{
objModel = GetDefineModelByEdm();
if (objModel != null)
{
//int ModelCache = Convert.ToInt32(ConfigurationManager.AppSettings["ModelCache"]);
HttpRuntime.Cache.Insert(CacheKey, objModel, null, DateTime.Now.AddMinutes(), TimeSpan.Zero);
}
}
return objModel;
}

3 WebForm中所写最简单的缓存:

public partial class WebForm_cache : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region 缓存
List<object> list = (List<object>)HttpRuntime.Cache["tc_students"];
if (list == null || list.Count <= ) //如果缓存中没有就从DB中获取
{
List<object> listDB = new MyORM_BLL().SelectAllModel(typeof(TC_STUDENT));
if (listDB.Count > ) //把数据库中获得的数据进行缓存
{
HttpRuntime.Cache.Insert("tc_students", listDB, null, DateTime.Now.AddSeconds(), TimeSpan.Zero);
}
list = listDB;
}
Repeater1.DataSource = list;
Repeater1.DataBind();
#endregion
}
} WebForm_cache.aspx.cs

参考:

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

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

最新文章

  1. python中的Ellipsis
  2. 我的屌丝giser成长记-工作篇之A公司
  3. Windows7 忘记密码的解决办法
  4. EditText键盘弹出时,会将布局底部的导航条顶上去(解决方法之一)
  5. collection view 开发笔记
  6. github不小心同步覆盖了本地文件
  7. Windows7 sp1 64位下安装配置eclipse+jdk+CDT+minGW
  8. Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai
  9. 五子棋Web版的开发(三)
  10. Android NDK进入发展
  11. dos cmd重启2003命令shutdown -r -t 0
  12. IO口输入输出模式理解
  13. springboot整合mq接收消息队列
  14. MVC编程实例----简易电子商务网站(一)
  15. embed标签详解
  16. [转] iOS9系统自带字体
  17. PHP-问题处理验证码无法显示出来
  18. [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存
  19. Arduino IDE for ESP8266 项目(2)wifi扫描
  20. Hive 组内计无重复数,追加每条记录后面

热门文章

  1. awk中使用shell变量
  2. 如何通过http接口使用f12b实现批量提交链接
  3. Redux API之bindActionCreators
  4. DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
  5. Centos7 搭建DNS服务器与原理配置详解
  6. hive学习1(hive基本概念)
  7. Mysql -- SQL常用命令实例
  8. (python)循环中动态产生变量
  9. Overlay网络与物理网络的关系
  10. 页面js框架