System.Web.Caching

來源:互聯網
上載者:User

標籤:let   string   typeof   returns   turn   sed   pre   ==   封裝   

System.Web.Caching簡單封裝類:

  1 using System;  2 using System.Collections.Generic;  3 using System.Web.Caching;  4 using System.Web;  5 using System.Collections;  6   7 namespace APP.HttpCache  8 {  9     public class CacheHelper 10     { 11         private static Cache cacheObj = HttpRuntime.Cache; 12  13         /// <summary> 14         /// 簡單key,vlaue寫入 15         /// </summary> 16         /// <param name="key"></param> 17         /// <param name="value"></param> 18         public static void Insert(string key, object value) 19         { 20             cacheObj.Insert(key, value); 21         } 22  23         /// <summary> 24         /// 設定絕對到期時間 25         /// </summary> 26         /// <param name="key"></param> 27         /// <param name="value"></param> 28         /// <param name="absoluteExpiration"></param> 29         /// <param name="slidingExpiration"></param> 30         public static void Insert(string key, object value, DateTime absoluteExpiration) 31         { 32             cacheObj.Insert(key, value, null, absoluteExpiration, Cache.NoSlidingExpiration); 33         } 34  35         /// <summary> 36         /// 設定平滑到期 37         /// </summary> 38         /// <param name="key"></param> 39         /// <param name="value"></param> 40         /// <param name="slidingExpiration"></param> 41         public static void Insert(string key, object value, TimeSpan slidingExpiration) 42         { 43             cacheObj.Insert(key, value, null, Cache.NoAbsoluteExpiration, slidingExpiration); 44         } 45  46         /// <summary> 47         /// 得到vlaue 48         /// </summary> 49         /// <param name="key"></param> 50         /// <returns></returns> 51         public static object Get(string key) 52         { 53             return cacheObj.Get(key); 54         } 55  56         /// <summary> 57         /// 得到vlaue 58         /// </summary> 59         /// <param name="key"></param> 60         /// <returns></returns> 61         public static T Get<T>(string key) 62         { 63             var v = cacheObj.Get(key); 64             return v == null ? default(T) : (T)Convert.ChangeType(v, typeof(T)); 65         } 66  67         /// <summary> 68         /// 移除key 69         /// </summary> 70         /// <param name="key"></param> 71         /// <returns></returns> 72         public static void Delete(string key) 73         { 74             cacheObj.Remove(key); 75         } 76  77         /// <summary> 78         /// 移除key 79         /// </summary> 80         /// <param name="key"></param> 81         /// <returns></returns> 82         public static object Remove(string key) 83         { 84             return cacheObj.Remove(key); 85         } 86  87         /// <summary> 88         /// 移除key 89         /// </summary> 90         /// <param name="key"></param> 91         /// <returns></returns> 92         public static T Remove<T>(string key) 93         { 94             var v = cacheObj.Remove(key); 95             return v == null ? default(T) : (T)Convert.ChangeType(v, typeof(T)); 96         } 97  98         /// <summary> 99         /// 緩衝key數量100         /// </summary>101         public static int KeyCount102         {103             get104             {105                 return cacheObj.Count;106             }107         }108 109         /// <summary>110         /// 所有key111         /// </summary>112         public static ArrayList KeyAll()113         {114             var arr = new ArrayList();115             var item = cacheObj.GetEnumerator();116             while (item.MoveNext())117             {118                 arr.Add(item.Key);119             }120             return arr;121         }122 123         /// <summary>124         /// 清空所有緩衝125         /// </summary>126         public static void DeleteAll()127         {128             var item = cacheObj.GetEnumerator();129             while (item.MoveNext())130             {131                 cacheObj.Remove(item.Key.ToString());132             }133         }134     }135 }
View Code

 

System.Web.Caching

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.