·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.Net Cache(缓存)—ASP.NET细枝末节(2)

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

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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(); }