C # ObjectCache and OutputCache Cache,

Source: Internet
Author: User
Tags form post

C # ObjectCache and OutputCache Cache,

1 /// <summary> 2 // Cache operation class 3 /// </summary> 4 public class Cache 5 {6 private static List <string> _ allUseCacheKey = new List <string> (); 7 private static ObjectCache _ cache = MemoryCache. default; 8 9 // <summary> 10 // Add cache 11 /// </summary> 12 // <param name = "key"> Key unique </param> 13 // <param name = "value"> value </param> 14 // <param name = "cacheOffset"> timeout </param> 15 public static void Add (string key, object value, DateTimeOffset cacheOffset) 16 {17 if (_ allUseCacheKey. contains (key) 18 {19 Remove (key); 20} 21 _ allUseCacheKey. add (key); 22 _ cache. add (key, value, cacheOffset ); 23} 24 25 // <summary> 26 // remove cache 27 /// </summary> 28 // <param name = "key"> Key </param> 29 public static void Remove (string key) 30 {31 if (_ allUseCacheKey. contains (key) 32 {33 _ allUseCacheKey. remove (key); 34} 35 _ cache. remove (key ); 36} 37 38 // <summary> 39 // read cache 40 // </summary> 41 // <param name = "key"> Key </param> 42 // <returns> </returns> 43 public static object Read (string key) 44 {45 if (_ allUseCacheKey. contains (key) 46 return _ cache [key]; 47 return null; 48} 49 50 // <summary> 51 // Clear all caches 52 // </summary> 53 public static void Clear () 54 {55 foreach (string value in _ allUseCacheKey) 56 {57 _ cache. remove (value); 58} 59 _ allUseCacheKey. clear (); 60} 61}
Public class SysuserStorageService {private int _ adminId; public SysuserStorageService (int adminId) {_ adminId = adminId ;} /// <summary> /// cache some content /// </summary> public void Storage () {tbl_admin user = new tbl_admin (); // obtain the object Cache as needed. add (_ adminId + "-Admin-" + SystemSetting. wms, user, SystemSetting. offSetTime); Cache. add (_ adminId + "-LastLoginTime-" + SystemSetting. wms, DateTime. now, SystemSetting. offSetTime);} public tbl_admin GetAdmin {get {return (tbl_admin) Cache. read (_ adminId + "-Admin-" + SystemSetting. wms) ;}} public string GetLastLoginTime {get {return (DateTime) Cache. read (_ adminId + "-LastLoginTime-" + SystemSetting. wms )). toString ("yyyy-MM-dd HH: mm: ss ");}}}
Public class SystemSetting {// <summary> // cache definition // </summary> public const string ShopWms = "Wms "; /// <summary> /// cache timeout /// </summary> public static DateTimeOffset OffSetTime = DateTimeOffset. now. addHours (24); // <summary> // log on to the Session // </summary> public const string LoginSession = "";}

SysuserStorageService storage = new SysuserStorageService (AdminId); // call
Tbl_admin User = storage. GetAdmin;

 

// Page Cache

[OutputCache (duration= 300, VaryByParam = "productno; brand; years")]

The above Code uses the @ OutputCatch command to declare the page cache. The page will be cached for 300 seconds. The @ OutputCatch command contains 10 attributes. These attributes can be used to set cache for different page conditions. common attributes are as follows:

 

CacheProfile:Obtain or set the OutputCacheProfile name.

Duration:Obtain or set the time that the cache entry needs to be retained in the cache.

VaryByHeader:Gets or sets the names of all well-separated HTTP headers used to change cache items.

Location:Gets or sets a value that determines the location of the cache item, including Any, Clint, Downstream, None, Server, and ServerAndClient. The default value is Any.

VaryByControl:Gets or sets a cluster of separated control identifiers that are contained in the current page or user control and used to change the current cache item.

NoStore:Gets or sets a value that determines whether the "Http Cache-Control: no-store" command is set.

VaryByCustom:Obtains the list of custom strings used by the output cache to change the cache items.

Enabled:Gets or sets a value indicating whether the output cache is enabled for the current content.

VaryByParam:Obtain the list of query string or form POST parameters.

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.