Usage of the ASP. NET Cache Cache

Source: Internet
Author: User
Tags httpcontext

This article is guided by: in. NET usage is often used in cache objects. There are HttpContext.Current.Cache and Httpruntime.cache,httpruntime.cache are application-level, and HttpContext.Current.Cache is defined for the current web context. HttpRuntime can be used in addition to the Web, non-web programs can also be used.

1, Httpruntime.cache equivalent is a cache specific implementation class, although this class is placed in the System.Web namespace. But non-Web apps can be used as well.


2, Httpcontext.cache is the encapsulation of the above-mentioned cache class, as encapsulated in HttpContext, limited to only know HttpContext under the use, that is, only for WEB applications.

The Httpruntime.cache, in the condition of being able, try to use the Httpcontext.cache.

First, there are several rules for caching data


First, data may be used frequently, and this data can be cached.


Second, the frequency of access to data is very high, or the frequency of access to a data is not high, but it has a long life cycle, such data is best to cache.


The third is a frequently overlooked problem, sometimes we cache too much data, usually on a X86 machine, if you want to cache more than 800M of data, there will be a memory overflow error. So the cache is limited. In other words, you should estimate the size of the cache set, limit the size of the cache set to less than 10, or it may cause problems. In ASP., a memory overflow error is reported if the cache is too large, especially if the large dataset object is cached.

You should carefully analyze your program. According to the actual situation to see where to use, where should not use. For example, using too much cache will increase the pressure on the server. Full-page output caching can also affect data updates. If you really need to cache a lot of data, you can consider static technology.

Second, the following introduction Httpruntime.cache common methods

C # codeCopy
Using System;
Using System.Web;
Using System.Collections;
public class Cookieshelper
{<summary>///Get data cache///</summary>//<param name= "CacheKey" > key </param> Public St Atic Object GetCache (String CacheKey){System.Web.Caching.Cache Objcache = Httpruntime.cache;    return Objcache[cachekey]; }<summary>///Set data cache///</summary> public static void Setcache (String CacheKey, Object Objobject ){System.Web.Caching.Cache Objcache = Httpruntime.cache;    Objcache.insert (CacheKey, objobject); }<summary>///Set data cache///</summary> public static void Setcache (String CacheKey, Object Objobject, Timespa n Timeout){System.Web.Caching.Cache Objcache = Httpruntime.cache; Objcache.insert (CacheKey, objobject, NULL, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, NULL); }<summary>///Set data cache///</summary> public static void Setcache (String CacheKey, Object Objobject, Datetim E absoluteexpiration, TimeSpan slidingexpiration){System.Web.Caching.Cache objcache = Httpruntime.cache; Objcache.insert (CacheKey, objobject, NULL, Absoluteexpiration, slidingexpiration); } ///<summary>///Remove the specified data cache///</summary> public static void Removeallcache (string CacheKey) {syste M.web.caching.cache _cache = Httpruntime.cache; _cache. Remove (CacheKey); } ///<summary>///Remove all caches///</summary> public static void Removeallcache () {System.Web.Caching.Cac He _cache = Httpruntime.cache; IDictionaryEnumerator cacheenum = _cache. GetEnumerator (); while (Cacheenum.movenext ()) {_cache. Remove (CacheEnum.Key.ToString ()); } }}

Usage of the ASP. NET Cache Cache

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.