首页 > 学院 > 开发设计 > 正文

ASP.Net Cache(缓存)—ASP.NET细枝末节(2)

2019-11-17 02:02:25
字体:
来源:转载
供稿:网友

asp.net Cache(缓存)—ASP.NET细枝末节(2)

概述

1.意义

把数据放到Cache中,在指定的时间内,可以直接从Cache中获取,避免对数据库等的压力。

2.做法

设置:

HttPRuntime.Cache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration);

读取:

HttpRuntime.Cache[“name”]

Demo

protected void Page_Load(object sender, EventArgs e) { //Cache是全局共享的 DataTable dt = (DataTable)HttpRuntime.Cache["persons"]; //如果Cache中没有,再去数据库中查询 //这样可以降低数据库服务器的压力 if (dt == null) { dt = SqlHelper.ExecuteQuery("select * from T_Persons"); //存储缓存,30秒后过期 HttpRuntime.Cache.Insert("persons", dt, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero); } Repeater1.DataSource = dt; Repeater1.DataBind(); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表