ASP.NET-【緩衝】-使用ASP.NET緩衝

來源:互聯網
上載者:User

標籤:

緩衝一個List 泛型結構

1.顯示
            var s = Get("personsl");            foreach (var item in s)            {                Response.Write(item.Name);            }

2.獲得資料

        //獲得資料        public List<Personal> Get(string key)        {            List<Personal> list = DTcms.Common.CacheHelper.Get<List<Personal>>(key);            if (list == null || list.Count == 0)            {                DTcms.Common.CacheHelper.Insert("personsl", Create(), 30);                list = DTcms.Common.CacheHelper.Get<List<Personal>>(key);            }            return list;        }

3.建立資料來源

        //建立資料來源        public List<Personal> Create()        {            List<Personal> list = new List<Personal>();            for (int i = 0; i < 10; i++)            {                Personal p = new Personal(i.ToString(), "xiaoming" + i);                list.Add(p);            }            return list;        }
    //資料對象    public class Personal    {        public string ID { get; set; }        public string Name { get; set; }        public Personal(string id, string name)        {            this.ID = id;            this.Name = name;        }    }

4.查看緩衝個數

//異常不能樣本話的
Response.Write(new System.Web.Caching.Cache().Count);
Response.Write(HttpRuntime.Cache.Count);

5.Cache 的絕對到期與滑動到期
絕對到期:設定絕對到期時間 到了指定時間以後會失效。(類似Cookie機制)
相對到期也稱滑動到期:設定相對到期時間 指定時間內無訪問會失效。(類似Session機制)

HttpRuntime.Cache與HttpContext.Current.Cache 為同一個對象
HttpRuntime.Cache.Add 存在相同的鍵會異常,返回緩衝成功的對象 HttpRuntime.Cache.Insert存在相同的鍵會替換無傳回值

HttpRuntime.Cache["key"] 使用字典的方式也可以讀取和設定
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero); //設定絕對到期時間 到了指定時間以後會失效 ps: TimeSpan.Zero == System.Web.Caching.Cache.NoSlidingExpiration
HttpRuntime.Cache.Insert(key, value, null, DateTime.MaxValue, TimeSpan.FromSeconds(seconds)); //設定相對到期時間 指定時間內無訪問會失效 ps: DateTime.MaxValue == System.Web.Caching.Cache.NoAbsoluteExpiration

ASP.NET-【緩衝】-使用ASP.NET緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.