Usage instructions for. NET Cache Design _ Practical Tips

Source: Internet
Author: User
Tags httpcontext

About caching design
1, under what circumstances with the cache

Caching is one of the best ways to improve application performance. The use of caching can optimize the data query, avoid unnecessary network data return, and avoid the implementation of unnecessary identical data processing logic. When implementing caching, we want to determine when to mount the cached data. Avoid client data latency with asynchronous mount caching or batch processing.
In general, the same business logic is requested within a certain period of time without change, the cache can be used to design. Requests for data requests are not suitable for caching, such as forum replies, but the topic of the Forum can be designed using caching.


2, the cache design steps
Determine the cache data structure: Which data in the design is used to cache, design the caching structure of the data
Determine what data is cached
Determine cache expiration rules and cleanup
Determining how to mount cached data


3. Example Community Server Cache class


Copy Code code as follows:

Using System;
Using System.Collections;
Using System.Text.RegularExpressions;
Using System.Web;
Using System.Web.Caching;

Namespace Larry.cache
{
<summary>
Cache class Community Server cache classes
</summary>
public class Basecache
{
<summary>
CacheDependency description
If you add an item with a dependency to the Cache, when the dependency changes, the
The item is automatically removed from the Cache. For example, suppose you add an item to the Cache,
and make it dependent on an array of file names. When a file in the array changes, the
The items associated with the array are removed from the cache.
[C #]
Insert the cache item.
CacheDependency dep = new CacheDependency (fileName, DT);
Cache. Insert ("Key", "value", DEP);
</summary>
public static readonly int dayfactor =;
public static readonly int hourfactor =;
public static readonly int minutefactor =;
public static readonly Double secondfactor = 0.;

private static readonly System.Web.Caching.Cache _cache;

private static int Factor =;

<summary>
Single-piece mode
</summary>
Static Basecache ()
{
HttpContext context = HttpContext.Current;
If (context!= null)
{
_cache = context. Cache;
}
Else
{
_cache = Httpruntime.cache;
}
}

<summary>
Clear all caches at once
</summary>
public static void Clear ()
{
IDictionaryEnumerator cacheenum = _cache. GetEnumerator ();
ArrayList al = new ArrayList ();
while (Cacheenum.movenext ())//clear individually
{
Al. ADD (Cacheenum.key);
}

foreach (String key in AL)
{
_cache. Remove (key);
}

}



public static void Removebypattern (string pattern)
{
IDictionaryEnumerator cacheenum = _cache. GetEnumerator ();
Regex regex = new Regex (Pattern, regexoptions.ignorecase | Regexoptions.singleline | regexoptions.compiled);
while (Cacheenum.movenext ())
{
if (regex). IsMatch (CacheEnum.Key.ToString ()))
_cache. Remove (CacheEnum.Key.ToString ());
}
}

<summary>
Clear a specific cache
</summary>
<param name= "Key" ></param>
public static void Remove (String key)
{
_cache. Remove (key);
}

<summary>
Cache object.
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
public static void Insert (string key, Object obj)
{
Insert (key, obj, NULL,);
}

       ///<summary>
       / Cache obj and establish dependencies
       ///</summary>
        ///<param name= "key" ></PARAM>
       /// <param name= "obj" ></param>
       ///<param name= "DEP" > </PARAM>
        public static void Insert (string key, Object obj, CacheDependency dep)
        {
             Insert (key, obj, DEP, Minutefactor *);
       }

       ///<summary>
       / Cache objects by seconds
       ///</summary>
        ///<param name= "key" ></PARAM>
       ///<param Name= "obj" ></param>
       ///<param name= "seconds" ></ PARAM>
        public static void Insert (string key, object obj, int seconds) br>        {
             Insert (key, obj, null, seconds);
       }

       ///<summary>
       / Cache objects in seconds and store priority
       ///</summary>
        ///<param name= "key" ></PARAM>
       /// <param name= "obj" ></param>
       ///<param name= "seconds" ></param>
       ///<param name= "Priority" ></param>
        public static void Insert (string key, object obj, int seconds, CacheItemPriority priority)
        {
             Insert (key, obj, null, seconds, priority);
       }

<summary>
Cache objects by seconds and establish dependencies
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
<param name= "DEP" ></param>
<param name= "Seconds" ></param>
public static void Insert (string key, Object obj, cachedependency dep, int seconds)
{
Insert (key, obj, dep, seconds, Cacheitempriority.normal);
}

<summary>
Cache objects in seconds and establish priority dependencies
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
<param name= "DEP" ></param>
<param name= "Seconds" ></param>
<param name= "Priority" ></param>
public static void Insert (string key, Object obj, cachedependency dep, int seconds, cacheitempriority priority)
{
if (obj!= null)
{
_cache. Insert (key, obj, DEP, DateTime.Now.AddSeconds (Factor * seconds), TimeSpan.Zero, priority, NULL);
}

}


        public static void Microinsert (string key, object obj, int Secondfactor)
        {
             if (obj!= null)
            {
                _cache. Insert (key, obj, NULL, DateTime.Now.AddSeconds (Factor * secondfactor), TimeSpan.Zero);
           }
       }

<summary>
Maximum Time cache
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
public static void Max (string key, Object obj)
{
Max (key, obj, null);
}

<summary>
Maximum time cache with dependencies
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
<param name= "DEP" ></param>
public static void Max (string key, Object obj, CacheDependency dep)
{
if (obj!= null)
{
_cache. Insert (key, obj, DEP, DateTime.MaxValue, TimeSpan.Zero, cacheitempriority.abovenormal, NULL);
}
}

<summary>
Insert an item into the cache for the Maximum allowed time
</summary>
<param name= "Key" ></param>
<param name= "obj" ></param>
public static void permanent (string key, Object obj)
{
Permanent (key, obj, null);
}

public static void permanent (string key, Object obj, CacheDependency dep)
{
if (obj!= null)
{
_cache. Insert (key, obj, DEP, DateTime.MaxValue, TimeSpan.Zero, cacheitempriority.notremovable, NULL);
}
}

public static object get (String key)
{
return _cache[key];
}

<summary>
return int of seconds * Secondfactor
</summary>
public static int secondfactorcalculate (int seconds)
{
Insert method below takes integer seconds, so we have to round any fractional values
Return Convert.toint (Math.Round (double) seconds * secondfactor));
}
}
}

In fact, this class is a monolithic pattern of the design and caching of public action methods, where cachedependency represents the establishment of a cache dependency, cacheitempriority represents the priority of the cache. S uses the following

Copy Code code as follows:

public static CardShop.Model.Systems GetConfig ()
{
Const string CacheKey = "Webconfig";
CardShop.Model.Systems samplecachetable = Larry.Cache.BaseCache.Get (CacheKey) as CardShop.Model.Systems;
if (samplecachetable = null)
{
Oprationcheck.message ("First load use cache");
Samplecachetable = model;
Larry.Cache.BaseCache.Insert (CacheKey, samplecachetable, Larry.Cache.BaseCache.MinuteFactor);
}
Else
{
Oprationcheck.message ("The cache has been loaded with no reload required");
}
return samplecachetable;
}

Related Article

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.