Implement data cache (similar to Cache function)

xiaoxiao2021-03-05  22

Using system.threading; using system.collections;

namespace MyCache {///

/// implement caching /// public sealed class MyCache {private static Hashtable Hash = Hashtable.Synchronized (new Hashtable ()); private static Thread thread; static MyCache () {Thread = new thread (new threadstart (autoremovedata); thread.start ();} ~ mycache () {hash.clear (); hash = null; thread.abort (); thread = null;}

Private static void autoredata () {while (turn (turn) {// Clear timeout No visual discussion version of the memory IF (((data) Hash [obj]) .time

Public Static Object Get (String Key) {IF (Hash [Key] == Null) Return Null; Else Return ((DATA) HASH [Key]). base;}

// More than a few minutes does not access: When each visit, change the expiration time (unciprocated code) Public Static Void Set (Object Basedata, String Key, Int Cachetime) {set (Basedata, Key, DateTime.now.Addminutes (Cachetime), NULL);

Public Static Void Set (Object Basedata, String Key, Int Cachetime, String Callback {set (Basedata, Key, DateTime.now.addminutes (Cachetime), Callback;

Public Static Void Set (Object Basedata, String Key, DateTime Removetime) {set (Basedata, Key, Removetime, Null)

Public Static Void Set (Object Basedata, String Key, DateTime Removetime, String Callback {if (Hash [Key]! = NULL) Remove (key); Data D = New Data (Basedata, Removetime, Callback); Hash.Add ( Key, d);

Private static void remove (String Key) {if (Hash [key] == null) Return; Data D = (DATA) HASH [key]; if (d.callback! = null & d.callback! = "" & D .Base.gettype (). GetMethod (d.callback)! = Null) {try {d.base.gettype (). InvokeMember (D.callback, System.Reflection.BindingFlags.InvokeMethod, Null, D.Base, NULL) Catch {}} Hash.Remove (key);} private datetime Time; public object base; public string callback = ""; public bool islast = false;

Public Data (Object Data, DateTime Removetime, String Callback) {this.base = data; this.time = removetime; this.callback = Callback;}}}}

转载请注明原文地址:https://www.9cbs.com/read-37781.html

New Post(0)