zoukankan      html  css  js  c++  java
  • .net 之缓存

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DBHelper;
    using System.Data;
    using MySql.Data.MySqlClient;
    using System.Web.Caching;
    
    public partial class 缓存过期 : System.Web.UI.Page
    {
        static bool itemremove = false;
        static CacheItemRemovedReason reson;
        CacheItemRemovedCallback onremove = null;
    
        public void removeCallback(string k, object v, CacheItemRemovedReason r) {
    
            itemremove = true;
            reson = r;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = (DataSet)Cache["students"];
            if (ds == null) {
                ds = GetStudent();
                onremove=new CacheItemRemovedCallback(this.removeCallback);
                CacheDependency cd=new CacheDependency(Server.MapPath("web.config"));
                Cache.Insert("students", ds, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), CacheItemPriority.High, onremove);
            
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Cache["students"] != null) {
                Cache.Remove("students");
            
            }
            if (itemremove)
            {
    
                Label1.Text += "REmove触发";
                Label1.Text += "<br>";
                Label1.Text += reson.ToString();
            }
            else {
                Label1.Text += Server.HtmlEncode(Cache["students"] as string);
            
            }
        }
    
        public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
                CacheKey,
                objObject,
                dep,
                System.Web.Caching.Cache.NoAbsoluteExpiration, //从不过期
                System.Web.Caching.Cache.NoSlidingExpiration, //禁用可调过期
                System.Web.Caching.CacheItemPriority.Default,
                null);
        }
    
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }
    
        public DataSet GetStudent() 
        {
            string sql = "select * from t_student";
            return SqlHelper.ExecuteDataSetText(sql);
        }
    
    
    }
    

      

  • 相关阅读:
    angular11源码探索七[服务二]
    angular11源码探索六[服务基础一]
    有趣的特效,嘤嘤嘤
    angular11学习(十八)
    matplotlib 去掉小方框
    xlrd.biffh.XLRDError 问题报错
    页面点击出现蓝色背景色
    移动端不显示滚动条
    Swiper垂直方向滑动,高度获取不正确的解决方法
    二维树状数组
  • 原文地址:https://www.cnblogs.com/mengluo/p/6077764.html
Copyright © 2011-2022 走看看