码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-03-20 01:13:20      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

概述

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

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

标签:

原文地址:http://www.cnblogs.com/mcad/p/4352303.html

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