分享

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

 昵称10504424 2015-03-20

概述

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多