. NET cache design instructions

Source: Internet
Author: User

Cache Design
1. When to use the cache

Caching is one of the best ways to improve application performance. Cache can be used to optimize data query, avoid unnecessary network data return, and avoid unnecessary identical data processing logic. When implementing the cache, we need to determine when to load the cache data. Use asynchronous loading of cache or batch processing to avoid client data delay.
In general, if the same business logic is requested within a certain period of time without changes, the cache can be used for design. Cache is not suitable for requests with frequent data requests, such as forum replies. However, cache can be used for Forum topics.


2. cache design steps
Determine the cache data structure: that is, the data used in the design is cached, And the cache structure of the data is designed.
Determine what data to cache
Determine cache expiration rules and clear
Determine how to load cache data


3. cache class for example Community Server


Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Text. RegularExpressions;
Using System. Web;
Using System. Web. Caching;

Namespace Larry. Cache
{
/// <Summary>
/// Cache class the cache class of Community Server
/// </Summary>
Public class BaseCache
{
/// <Summary>
/// CacheDependency description
/// If you add a dependency to the Cache, when the dependency is changed,
/// This item will be automatically deleted from the Cache. For example, if you add an item to the Cache,
/// And make it dependent on the file name array. When a file in the array is changed,
/// Items associated with the array will be deleted 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 one time
/// </Summary>
Public static void Clear ()
{
IDictionaryEnumerator CacheEnum = _ cache. GetEnumerator ();
ArrayList al = new ArrayList ();
While (CacheEnum. MoveNext () // clear one by one
{
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 the specified 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 create dependency
/// </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 second
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "obj"> </param>
/// <Param name = "seconds"> </param>
Public static void Insert (string key, object obj, int seconds)
{
Insert (key, obj, null, seconds );
}

/// <Summary>
/// Cache objects by second and store the 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 second and create 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 the object by second and create a priority dependency
/// </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 single-piece mode design and cache public operation method. CacheDependency indicates the establishment of cache dependencies, and CacheItemPriority indicates the cache priority. S is used as follows

Copy codeThe Code is 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 loading and Using Cache ");
SampleCacheTable = model;
Larry. Cache. BaseCache. Insert (cacheKey, sampleCacheTable, 24 * Larry. Cache. BaseCache. MinuteFactor );
}
Else
{
OprationCheck. Message ("cache loaded does not need to be loaded again ");
}
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.