zoukankan      html  css  js  c++  java
  • 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();
            }

  • 相关阅读:
    在Ubuntu上安装PHPStudy组件
    手把手教你在Ubuntu上分别安装Nginx、PHP和Mysql
    ErrorKiller:Failed to decode response: zlib_decode(): data error
    HTTP和FTP上传文件的区别
    关于HTTP,你知道哪些?
    史上最全的FTP网址
    深入解读TCP/IP
    nefu 462 fib组合
    MZL's xor
    The Highest Mark(01背包)
  • 原文地址:https://www.cnblogs.com/mcad/p/4352303.html
Copyright © 2011-2022 走看看