C # cache operation class asp.net for syntactic sugar,

Source: Internet
Author: User
Tags delete cache

C # cache operation class asp.net for syntactic sugar,

Considering that I will write session cookies and other operation classes below, they share common characteristics with cache. Therefore, IHttpStorageObject abstract class is inherited to ensure the consistency of the function style. However, to facilitate the call, a singleton is used in the abstract to simplify the call.

 

 

The usage is simple:

Using System; using System. collections. generic; using System. linq; using System. text; using System. web; using System. web. caching; using System. collections; using System. linq. expressions; namespace SyntacticSugar {// <summary> /// ** Description: cache operation class // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan // ** usage instructions: http://www.cnblogs.com/sunkaixuan/p/4563462.html /// </Summary> /// <typeparam name = "K"> key </typeparam> /// <typeparam name = "V"> value </typeparam> public class CacheManager <V>: IHttpStorageObject <V >{# region global variable private static CacheManager <V> _ instance = null; private static readonly object _ instanceLock = new object (); # endregion # region constructor private CacheManager () {}# endregion # region attributes /// <summary> /// obtain value based on key /// </summary> /// <value> </v Alue> public override V this [string key] {get {return (V) HttpRuntime. cache [CreateKey (key)] ;}} # endregion # region Public Function // <summary> // check whether the key exists // </summary> /// <param name = "key"> key </param >/// <returns> /// <c> true </c> does not exist <c> false </c>. ///// </returns> public override bool ContainsKey (string key) {return HttpRuntime. cache [CreateKey (key)]! = Null ;} /// <summary> /// obtain the cache value /// </summary> /// <param name = "key"> key </param> /// <returns> </returns> public override V Get (string key) {return (V) HttpRuntime. cache. get (CreateKey (key);} // <summary> // obtain the instance (Singleton Mode) /// </summary> /// <returns> </returns> public static CacheManager <V> GetInstance () {if (_ instance = null) lock (_ instanceLock) if (_ instance = null) _ instance = new CacheManager <V> (); return _ instance;} // <summary> // insert cache (20 minutes by default) /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value </param> public override void Add (string key, V value) {Add (key, value, Minutes * 20 );} /// <summary> /// insert cache /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value </param> // <param name = "cacheDurationInSeconds"> expiration time in seconds </param> public void Add (string key, V value, int cacheDurationInSeconds) {Add (key, value, cacheDurationInSeconds, CacheItemPriority. default);} // <summary> // insert cache. /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value </param>/ // <param name = "cacheDurationInSeconds"> expiration time in seconds </param> // <param name = "priority"> cache item attribute </param> public void Add (string key, V value, int cacheDurationInSeconds, CacheItemPriority priority) {string keyString = CreateKey (key); HttpRuntime. cache. insert (keyString, value, null, DateTime. now. addSeconds (cacheDurationInSeconds), Cache. noSlidingExpiration, priority, null) ;}/// <summary> /// insert cache. /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value </param>/ // <param name = "cacheDurationInSeconds"> expiration time in seconds </param> // <param name = "priority"> cache item attribute </param> public void Add (string key, V value, int cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority) {string keyString = CreateKey (key); HttpRuntime. cache. insert (keyString, value, dependency, DateTime. now. addSeconds (cacheDurationInSeconds), Cache. noSlidingExpiration, priority, null );} /// <summary> /// Delete cache /// </summary> /// <param name = "key"> key </param> public override void Remove (string key) {HttpRuntime. cache. remove (CreateKey (key) ;}/// <summary> /// clear all caches /// </summary> public override void RemoveAll () {System. web. caching. cache cache = HttpRuntime. cache; IDictionaryEnumerator CacheEnum = cache. getEnumerator (); ArrayList al = new ArrayList (); while (CacheEnum. moveNext () {al. add (CacheEnum. key);} foreach (string key in al) {cache. remove (key );}} /// <summary> /// clear all caches containing keywords /// </summary> /// <param name = "removeKey"> keyword </param> public override void RemoveAll (Func <string, bool> removeExpression) {System. web. caching. cache _ cache = HttpRuntime. cache; var allKeyList = GetAllKey (); var delKeyList = allKeyList. where (removeExpression ). toList (); foreach (var key in delKeyList) {Remove (key );}} /// <summary> /// obtain all cache keys /// </summary> /// <returns> </returns> public override IEnumerable <string> GetAllKey () {IDictionaryEnumerator CacheEnum = HttpRuntime. cache. getEnumerator (); while (CacheEnum. moveNext () {yield return CacheEnum. key. toString ();}} # endregion # region private function // <summary> // create a KEY /// </summary> /// <param name = "key"> Key </param> /// <returns> </returns> private string CreateKey (string key) {return "http_cache _" + key. toString () ;}# endregion }}

  

using System;namespace SyntacticSugar{    public abstract class IHttpStorageObject<V>    {        public int Minutes = 60;        public int Hour = 60 * 60;        public int Day = 60 * 60 * 24;        public System.Web.HttpContext context = System.Web.HttpContext.Current;        public abstract void Add(string key, V value);        public abstract bool ContainsKey(string key);        public abstract V Get(string key);        public abstract global::System.Collections.Generic.IEnumerable<string> GetAllKey();        public abstract void Remove(string key);        public abstract void RemoveAll();        public abstract void RemoveAll(Func<string, bool> removeExpression);        public abstract V this[string key] { get; }    }}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.