C# Cache cache read settings

First create a CacheHelper.cs class with the following code :

 

[csharp] view plain copy
  1. using System;  
  2. using System.Web;  
  3. using System.Collections;  
  4. using System.Web.Caching;  
  5.   
  6. public class CacheHelper  
  7. {  
  8.     /// <summary>  
  9.     /// Get the data cache  
  10.     /// </summary>  
  11.     /// <param name="cacheKey">键</param>  
  12.     public static object GetCache(string cacheKey)  
  13.     {  
  14.         var objCache = HttpRuntime.Cache.Get(cacheKey);  
  15.         return objCache;  
  16.     }  
  17.     /// <summary>  
  18.     /// Set up the data cache  
  19.     /// </summary>  
  20.     public static void SetCache(string cacheKey, object objObject)  
  21.     {  
  22.         var objCache = HttpRuntime.Cache;  
  23.         objCache.Insert(cacheKey, objObject);  
  24.     }  
  25.     /// <summary>  
  26.     /// Set up the data cache  
  27.     /// </summary>  
  28.     public static void SetCache(string cacheKey, object objObject, int timeout = 7200)  
  29.     {  
  30.         try  
  31.         {  
  32.             if (objObject == null) return;  
  33.             var objCache = HttpRuntime.Cache;  
  34.             //relatively expired  
  35.             //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);  
  36.             //absolute expiration time  
  37.             objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);  
  38.         }  
  39.         catch (Exception)  
  40.         {  
  41.             //throw;  
  42.         }  
  43.     }  
  44.     /// <summary>  
  45.     /// Remove the specified data cache  
  46.     /// </summary>  
  47.     public static void RemoveAllCache(string cacheKey)  
  48.     {  
  49.         var cache = HttpRuntime.Cache;  
  50.         cache.Remove(cacheKey);  
  51.     }  
  52.     /// <summary>  
  53.     /// Remove all caches  
  54.     /// </summary>  
  55.     public static void RemoveAllCache()  
  56.     {  
  57.         var cache = HttpRuntime.Cache;  
  58.         var cacheEnum = cache.GetEnumerator();  
  59.         while (cacheEnum.MoveNext())  
  60.         {  
  61.             cache.Remove(cacheEnum.Key.ToString());  
  62.         }  
  63.     }  
  64. }  


The citations are also posted above, just a few.

Then call:

 

 

[csharp] view plain copy
  1. public IEnumerable<CompanyModel> FindCompanys()  
  2.         {  
  3.             var cache = CacheHelper.GetCache( "commonData_Company"); //Read first  
  4.             if (cache ==  null) //if there is no such cache  
  5.             {  
  6.                 var queryCompany = _base.CompanyModel(); //fetch from database  
  7.                 var enumerable = queryCompany.ToList();  
  8.                 CacheHelper.SetCache( "commonData_Company", enumerable); //Add cache  
  9.                 return enumerable;  
  10.             }  
  11.             var result = (List<CompanyModel>)cache; //Return to the cache directly if there is one  
  12.             return result;  
  13.         }  


The test results are also posted here to see:


The first load is null, and then the database is read and added to the cache. The current return to the foreground is the data taken from the database.


Refresh the page and find that there are already 30 pieces of data read out in the cache,


Then go ahead and return the data in the cache:

roughly these.

End

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889514&siteId=291194637