zoukankan      html  css  js  c++  java
  • System.Web.Caching.Cache类 缓存

    1.文件缓存依赖

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Cache cache = HttpContext.Current.Cache;
                //文件缓存依赖
                cache.Insert("CC", "依赖项测试", new CacheDependency(@"D:123.txt"));
                //这时候在about.aspx页面添加一行代码,当更改一下D:123.txt时,cache["cc"]会立即被清空
            }
        }
    public partial class About : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //直接打开本页面,输出缓存依赖项测试
                //当更改D:123.txt之后,在刷新,输出空,表明该Cache是依赖于D:123.txt的
                Response.Write(HttpContext.Current.Cache["CC"]);
           

    2.NoSlidingExpiration 绝对过期时间

    注:NoSlidingExpiration  绝对过期时间,当超过设定时间,立即移除。

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Cache cache = HttpContext.Current.Cache;
                //30秒后就到期,立即移除,没商量
                cache.Insert("DD", "绝对过期测试", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
    public partial class About : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //先打开index.aspx添加到缓存 然后立即打开本页面,输出 绝对过期测试
                //持续刷新5秒后,不会再输出  绝对过期测试
                Response.Write(HttpContext.Current.Cache["DD"]);
            }
        }

    3.NoAbsoluteExpiration 滑动过期时间

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Cache cache = HttpContext.Current.Cache;
                //弹性过期时间,当缓存没使用10秒就过期
                cache.Insert("DD", "滑动过期测试", null, System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(10));
            }
        }
    public partial class About : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //直接打开本页面,输出弹性过期测试
                //如果一直不停地刷新,都会继续输出,但是当超过10秒后再刷新,不会再输出   滑动缓存测试
                Response.Write(HttpContext.Current.Cache["DD"]);
            }
        }

    4.缓存的优先级设置

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Cache cache = HttpContext.Current.Cache;
                //文件权重级别
                cache.Add("MyData", "缓存重要级别", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null);
            }
        }
    //在服务器释放系统内存时,具有该优先级级别的缓存项最有可能被从缓存删除。
            Low = 1,//在服务器释放系统内存时,具有该优先级级别的缓存项比分配了 System.Web.Caching.CacheItemPriority.Normal
            //优先级的项更有可能被从缓存删除。
            BelowNormal = 2,//在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,其被删除的可能性仅次于具有 System.Web.Caching.CacheItemPriority.Low
            Normal = 3,//缓存项优先级的默认值为 System.Web.Caching.CacheItemPriority.Normal。
            Default = 3,//在服务器释放系统内存时,具有该优先级级别的缓存项被删除的可能性比分配了 System.Web.Caching.CacheItemPriority.Normal
            //优先级的项要小。
            AboveNormal = 4,//在服务器释放系统内存时,具有该优先级级别的缓存项最不可能被从缓存删除。
            High = 5,//在服务器释放系统内存时,具有该优先级级别的缓存项将不会被自动从缓存删除。但是,具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除
            NotRemovable = 6,
    优先级
    Low=1
    BelowNormal=2
    Normal=3
    Default=3
    AboveNormal=4
    High=5
    NotRemoveable=6

    5.当缓存被移除时,通知程序

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Cache cache = HttpRuntime.Cache;
                //文件权重级别
                cache.Add("MyData", "缓冲移除通知", null, DateTime.Now.AddSeconds(10) ,Cache.NoSlidingExpiration,CacheItemPriority.Low, Show);
            }
    
            public void Show(string key, object value, CacheItemRemovedReason reason)
            {
                Cache cache = HttpRuntime.Cache;
                Cache.Insert("MyData", "缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!");
            }
        }
    public partial class About : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(HttpRuntime.Cache["MyData"]);
            }
        }

     

  • 相关阅读:
    Leetcode 349. Intersection of Two Arrays
    hdu 1016 Prime Ring Problem
    map 树木品种
    油田合并
    函数学习
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 102. Binary Tree Level Order Traversal
    Leetcode 101. Symmetric Tree
    poj 2524 Ubiquitous Religions(宗教信仰)
    pat 1009. 说反话 (20)
  • 原文地址:https://www.cnblogs.com/sunxuchu/p/5433914.html
Copyright © 2011-2022 走看看