DataCache (dependent on File Cache and time cache, or both) in asp.net)

Source: Internet
Author: User
Tags delete cache

[Csharp] /// <summary> /// data cache class /// </summary> public class DataCache {# region cache depends on the file web. config private static string _ webconfigfile = string. empty; // <summary> // cache the web of dependent files. config // </summary> public static string webconfigfile {get {if (_ webconfigfile = string. empty) _ webconfigfile = HttpContext. current. server. mapPath ("/web. config "); return _ webconfigfile ;}} # endregion # region Delete cache // <summary> // Delete cache // </summary> // <param name = "CacheKey"> key </param> public static void DeleteCache (string CacheKey) {HttpRuntime. cache. remove (CacheKey );} # endregion # region obtain the cache /// <summary> /// obtain the cache /// </summary> /// <param name = "CacheKey"> key </param> /// <returns> </returns> public static object GetCache (string CacheKey) {return HttpRuntime. cache [CacheKey];} # endregion # region simple insert cache // <summary> /// simple insert cache /// </summary> /// <param name = "CacheKey"> key </param> // <param name = "objObject"> data </param> public static void SetCache (string CacheKey, object objObject) {HttpRuntime. cache. insert (CacheKey, objObject );} # endregion # insert cache data with an expiration time in region /// <summary> /// insert cache data with an expiration time /// </summary> /// <param name = "CacheKey"> key </param> // <param name = "objObject"> data </param> // <param name = "absoluteExpiration"> expiration time </param> /// <param name = "slidingExpiration"> scheduling parameters, if null is passed, the scheduling is disabled. </param> public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) {if (slidingExpiration = null) slidingExpiration. noSlidingExpiration; HttpRuntime. cache. insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration);} # endregion # region inserts cache data, specifying the number of seconds to cache /// <summary> /// Insert cache data, specify the number of seconds to cache /// </summary> /// <param name = "CacheKey"> cache key </param> /// <param name = "objObject"> cached data </param> /// <param name = "seconds"> cache seconds </param> /// <param name = "slidingExpiration"> If null is passed, it is disabled. schedule expiration </param> public static void SetCacheSecond (string CacheKey, object objObject, int seconds, TimeSpan slidingExpiration) {DateTime absoluteExpiration = DateTime. now. addSeconds (seconds); if (slidingExpiration = null) slidingExpiration = Cache. noSlidingExpiration; HttpRuntime. cache. insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration);} # endregion # cache of region dependent files, if the file is not modified, it will not expire. // <summary> // the cache of the dependent file, the file will not expire if it is not modified. // </summary> // <param name = "CacheKey"> key </param> // <param name = "objObject"> data </param> // <param name = "depfilename"> dependent file, call the variables in DataCache </param> public static void SetCacheDepFile (string CacheKey, object objObject, string depfilename) {// cache dependent object System. web. caching. cacheDependency dep = new System. web. caching. cacheDependency (depfilename); System. web. caching. cache objCache = HttpRuntime. cache; objCache. insert (CacheKey, objObject, dep, System. web. caching. cache. noAbsoluteExpiration, // never expires System. web. caching. cache. noSlidingExpiration, // disable the adjustable expiration System. web. caching. cacheItemPriority. default, null) ;}# endregion}

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.