Common cache components and cache Components

Source: Internet
Author: User

Common cache components and cache Components

There are many cache types, and the most common one is the memory cache. There are also many ways to implement the memory Cache, such as using static variables, such as Cache, but these methods only apply to a single Cache variable. Each Cache variable must be rewritten in one way, it cannot be universal. A general memory cache component is provided here, which does not need to be implemented for each cache.

Let's not talk much about it. first go to the Code:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. reflection; 5 using System. text; 6 using System. web; 7 using System. web. caching; 8 9 namespace Loncin. codeGroup10.Utility10 {11 /// <summary> 12 // common cache component 13 /// </summary> 14 public class CacheHelper15 {16 /// <summary> 17 /// get cache object 18 // </summary> 19 // <typeparam name = "T"> cache object </typeparam> 20 // <param name =" Dele "> Object Data Acquisition Method </param> 21 // <param name =" cacheKey "> cache keyword </param> 22 // <param name =" cacheDuration"> cache time (minutes) </param> 23 // <param name = "objs"> Object Data Acquisition Parameters </param> 24 // <returns> return object </returns> 25 public static T GetCacheData <T> (Delegate dele, string cacheKey, int cacheDuration, params object [] objs) 26 {27 // The cache is empty 28 if (HttpRuntime. cache. get (cacheKey) = null) 29 {30 // Method for retrieving object data 31 MethodInfo methodInfo = Dele. Method; 32 33 T result = (T) methodInfo. Invoke (dele. Target, objs); 34 35 if (result! = Null) 36 {37 // expiration time 38 DateTime cacheTime = DateTime. now. addMinutes (cacheDuration); 39 40 // Add cache 41 HttpRuntime. cache. add (cacheKey, result, null, cacheTime, Cache. noSlidingExpiration, CacheItemPriority. notRemovable, null); 42} 43} 44 45 return (T) HttpRuntime. cache [cacheKey]; 46} 47} 48}

1. the cache component method receives a delegate for obtaining the original data method, a cache key, a cache expiration time, and parameters for obtaining the original data method;

2. the Cache is implemented using HttpRuntime. Cache. This is a. net built-in Cache component that uses absolute Cache (you can also change it to slide Cache as needed );

3. The method first obtains data from the cache, but the cache does not obtain the data. It executes the original data method delegate to obtain the data and adds it to the cache.

Example:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using Loncin. codeGroup10.Utility; 6 7 namespace Loncin. codeGroup10.ConsoleTest. test 8 {9 /// <summary> 10 // General cache component Test 11 /// </summary> 12 public class CacheHelperTest13 {14 /// <summary> 15/ // Test Method 16 /// </summary> 17 public void Test () 18 {19 // get data 20 var testData = CacheHelper. getCacheData <List <int> (ne W Func <int, List <int> (GetTestData), "TestKey", 5, 10); 21 22 if (testData! = Null & testData. Count> 0) 23 {24 Console. WriteLine ("data retrieved successfully! "); 25} 26} 27 28 /// <summary> 29 // obtain raw data 30 /// </summary> 31 /// <param name = "count"> </param> 32 // <returns> </returns> 33 public List <int> GetTestData (int count) 34 {35 var testData = new List <int> (); 36 for (int I = 0; I <count; I ++) 37 {38 testData. add (I); 39} 40 41 return testData; 42} 43} 44}

Summary:

1. the cache component can be extended. The method for obtaining raw data can be to call the service, or to send Http requests;

2. the Cache method can also be extended, replacing HttpRuntime. Cache with Redis or other caches, or even encapsulating a Complex Cache with a multi-level Cache method, which makes it easier to use the Cache method.

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.