Cache classes implemented by dictionary caches

Source: Internet
Author: User

Recently used the cache, so I wrote a simple cache.

Namespace Jchen.ext has this method:

        ///<summary>///Is null or empty///</summary> /// <param name= "str" ></param> ///< returns></returns> public static bool IsNullOrEmpty (string  str) {if (str = = null | | str. Trim () = = return Span style= "color: #0000ff;" >truereturn false   
IsNullOrEmpty

UsingSystem;UsingSystem.Collections.Generic;UsingJchen.ext;Namespacejchen.caching{///<summary>///Cache///</summary>///<typeparam name= "T" ></typeparam>PublicClass cache<t>{///<summary>///Lock///</summary>PrivateStaticObject lockobj =NewObject();///<summary>///Default cache///</summary>Privatestatic Cache<t> _default =New Cache<t> (True);///<summary>///Cache for each instance///</summary>Private dictionary<String, t> Instancecache =Null;///<summary>///The default cache///</summary>PrivateStatic dictionary<String, t> Defaultcache =New dictionary<String, t>();///<summary>///Cache///</summary>PublicCache () {Instancecache =New dictionary<String, t>(); }///<summary>///Private structure, different from cache (), just to instantiate _default;///For the default cache, it is not required and cannot be instantiated Instancecache///</summary>///<param name= "Isdefualt" ></param>Private Cache (boolIsdefualt) {}///<summary>///Default cache instance relative to T///Special note: For the same type T,default is the same object///</summary>PublicStatic cache<t> Default {get {Return_default; } }///<summary>///Set or get Cached data///</summary>///<param name= "Key" >Key, cannot be empty</param>///<returns>If the key is empty, the return value is the default value of type T</returns>Public Tthis[StringKey] {Get{if (Instancecache! =Null) {If(Instancecache.containskey (key)) {ReturnInstancecache[key]; } }Else{If(Defaultcache.containskey (key)) {ReturnDefaultcache[key]; } }ReturnDefault(T); }Set{If(Key. IsNullOrEmpty ()) {ThrowNew ArgumentNullException ("Key","The key cannot be empty!"); }if (Instancecache! =Null) {If(Instancecache.containskey (key)) {Instancecache[key] =Value }Else{Lock(lockobj) {Instancecache.add (key, value);} } }Else{If(Defaultcache.containskey (key)) {Defaultcache[key] =Value }Else{Lock(lockobj) {Defaultcache.add (key, value);} } } } }///<summary>///Number of Caches///</summary>PublicIntCount {Get{if (Instancecache! =Null) {ReturnInstancecache.count; }Else{ReturnDefaultcache.count; } } }///<summary>///Removes the cached value of the specified key///</summary>///<param name= "Key" >Key, cannot be empty</param>///<returns></returns>PublicBOOL Remove (StringKey) {If(Key. IsNullOrEmpty ()) {ReturnFalse; }if (Instancecache! =Null) {If(Instancecache.containskey (key)) {ReturnInstancecache.remove (key); } }Else{Ifreturn Defaultcache.remove (key); }} return false///<summary> ///  empty cache ///</summary > public void Clear ( {if (Instancecache! = null else    
Cache

Cache classes implemented by dictionary caches

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.