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