Usually we cache for the page and related data (divided into the client and server side of the cache), the following code for the general operation of the corresponding cache (server), to reduce the number of accesses to the database, reduce the pressure on the servers.
(i) Cachehelper class
The Cachehelper class is primarily dependent on the System.Web.Caching.HostingEnvironment.Cache of the system, as follows:
public static class Cachehelper {private static Cache _cache;
public static double Savetime {get;
Set
Static Cachehelper () {_cache = Hostingenvironment.cache;
Savetime = 15.0; public static object get (String key) {if (string).
IsNullOrEmpty (key)) {return null; Return _cache.
Get (key);
public static T-get<t> (String key) {Object obj = Get (key);
return Obj==null?default (t):(T) obj; public static void Insert (string key, object value, CacheDependency dependency, cacheitempriority priority, CacheItemRemovedCallback callback) {_cache.
Insert (key, value, dependency, Cache.noabsoluteexpiration, Timespan.fromminutes (Savetime), priority, callback); } public static void Insert (string key, object value, CacheDependency dependency, CacheItemRemovedCallback callback) {
Insert (key, value, dependency, Cacheitempriority.default, callback); public static void Insert (string key, object value, CacheDependency dependency) {Inser
T (key, value, dependency, cacheitempriority.default, NULL); public static void Insert (string key, object value) {Insert (key, value, NULL, Cacheite
Mpriority.default, NULL); public static void Remove (string key) {if (string).
IsNullOrEmpty (key)) {return; } _cache.
Remove (key); public static ilist<string> Getkeys () {list<string> keys = new List<str
Ing> (); IDictionaryEnumerator enumerator = _cache.
GetEnumerator (); while (enumerator. MoveNext () {keys. ADD (Enumerator.
Key.tostring ()); return keys.
AsReadOnly ();
public static void RemoveAll () {ilist<string> keys = Getkeys (); foreach (string key in keys) {_cache.
Remove (key); }
}
}
}
On the original basis, the following 3 methods were added:
(1) public static T get<t> (string key)
(2) public static ilist<string> Getkeys ()
(3) public static void RemoveAll ()
So we can conveniently in the static method to the cache to do the appropriate operation.