Cachehelper cache control to reduce server pressure

Source: Internet
Author: User
Tags static class

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.

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.