Asp.net status management and Cache

Source: Internet
Author: User
Tags website performance
In my impression, we talk about State management, more about application, session, and so on, and seldom talk about cache. Of course, cache is not within the scope of State management. But recently I used a cache in my work and found that the cache is actually easier to use and more practical than other objects.
Let's first list the objects that we often talked about.
1. Server Side
Application
It is a global control and must be locked before use
Session
Each user has its own copy with an expiration time, but the expiration time is not properly controlled.

2. Client
Cookie
Each user has a cookie, which has a good expiration time, but has a size limit of 4 kb. the browser may not support or disable cookies.
Viewstate
Each page has a viewstate
Hiddlefield
Controls

Let's Talk About cache. There are generally three categories: page-level output cache, page segment cache, and data cache (Cache object). Here we talk about cache objects. The cache makes the application and cookie integrated. It opens up a space in the server's memory to store data. The task owner can access the cache and set the expiration time flexibly, you can even set events that expire.
the Cache Usage is similar to that of other objects. There are two ways to add a cache: Add and insert.
the inser method can use an optional parameter, that is, the default parameter, to add a cache.
the add () method can only add items not in the cache. If an existing item in the cache is added, it will fail (but will not throw an exception), and insert () the method can overwrite the original items.
unlike the application, you do not need to use the lock operation when inserting the cache. The cache will process the concurrency by itself.
cache. add (
keyname, // cache name
keyValue, // the object to be cached
dependencies, // dependency
absoluteexpiration, // absolute expiration time
slidingexpiration, // relative expiration time
priority, // priority
cacheitemremovedcallback); // events caused by cache expiration

when using cache, the dependency and expiration are mainly used. Otherwise, there will be no difference with the application. There are three types of Dependencies:
file dependencies and other dependencies are considered as one type, cachedependency object
file dependency:
cachedependency filedepends = new cachedependency (server. mappath ("northwind. XML ");
cache. insert ("gridviewdataset", dsgrid, filedepends);
other Dependencies:
string [] cachedependsarray = {"depend0", "depend1", "depend2 "};
cachedependency cachedepends = new cachedependency (null, cachedependsarray);
cache. insert ("gridviewdataset", dsgrid, cachedepends);
Used Together:
string [] filedependsarray = {server. mappath ("northwind. XML ") };
string [] cachedependsarray = {" depend0 "," depend1 "," depend2 "};
cachedependency cachedepends = new cachedependency (filedependsarray, cachedependsarray);
cache. insert ("gridviewdataset", dsgrid, cachedepends);

another dependency is the expiration time.
the expiration time has two parameters: absoluteexpiration and slidingexpiration.
absoluteexpiration can be used to set the absolute expiration time of the cache, for example:
cache. insert ("gridviewdataset", dsgrid, null, datetime. now. addminutes (30), cache. noslidingexpiration);
the cache will expire 30 minutes after it is added.
the relative expiration time of noslidingexpiration can be set. If the cache is not accessed within the time set by noslidingexpiration, the cache expires. If there is access within this time period, the cache expiration time is reset to the original value, for example, noslidingexpiration = 20
If the cache is not accessed within 20 minutes, the cache expires. If the cache is accessed every 19 minutes, the cache will never expire.
cache. insert ("maid", dsgrid, null, cache. noabsoluteexpiration, timespan. fromseconds (30);

note the following:
1. cache. noabsoluteexpiration is an enumeration that indicates no absolute expiration time
cache. noslidingexpiration indicates no relative expiration time
2. if both the absolute expiration time and relative expiration time are set, refer to the following:
If the slidingexpiration parameter is set to noslidingexpiration, disable and adjust the expiration time. If the slidingexpiration parameter is set to greater than zero, the absoluteexpiration parameter is set to the value included in the now and slidingexpiration parameters. If you request this item from the cache before the time specified by the absoluteexpiration parameter, the item will be cached again, and the absoluteexpiration will be set again to the value included in datetime. Now and the slidingexpiration parameter. If the item is not requested from the cache before the date in the absoluteexpiration parameter, the item is removed from the cache.

priority
indicates how to process the data in the cache when the server memory resources are insufficient.
notremovable: items with this priority will not be evicted.
high: items with this priority level are the least likely to be evicted.
abovenormal: items with this priority level are less likely to be evicted than items assigned normal priority.
default: This is equivalent to normal.
normal: the default value.
belownormal: items with th Is priority level are more likely to be evicted than items assigned normal priority.
low: items with this priority level are the most likely to be evicted.
although it is in English, it is easy to understand that the higher the setting, the harder it is to be cleared.

handle cache invalidation events
This requires a delegate: cachedependency. Of course, you can directly write the method name at the parameter.
this event is triggered when the cache fails and the method is called. The method has three parameters:
string STR, object sender, cacheitemremovedreason reason
usage:
Public static cacheitemremovedcallback oncallback = NULL;
protected void page_load (Object sender, eventargs E)
{< br> cachedependency Dep = new cachedependency (null, new string [] {"1", "2"});
oncallback = callback;
cache. insert ("A", "A", null, cache. noabsoluteexpiration, timespan. fromseconds (5), cacheitempriority. default, oncallback);
}

Private void callback (string STR, object sender, cacheitemremovedreason reason)
{
System. Io. file. writealltext (@ "C: \ a.txt", reason. tostring ());
}

Note:
At first, I wanted to give a prompt to the front-end when an event is triggered due to expiration. The idea is good, but it cannot be implemented in reality because Web access is stateless, it cannot know who set it, and it is public. Do you want to send a prompt to everyone online ?! It can only perform operations on the server, such as file writing and database access.

From the above perspective, it can speed upProgramResponse speed, performance improvement, and the existence of the server to solve security issues, and flexible setting of dependency conditions are not described in five words: very good and powerful!
But do not abuse the server resources!

Practical application:
We usually display the data in the database on the web, so if the cache can be associated with the database, it seems that the database is not mentioned above! So we have to change our mind.
Create a trigger. When the database changes, trigger the trigger to write files, and then associate the cache to this file, so that database changes can affect the extinction and generation of the cache.

below is my reference document, write more than me:
how to manage cache in Asp.net
http://www.cnblogs.com/aspnet2008/archive/2008/10/09/1307370.html
Asp. net Cache Usage
http://www.cnblogs.com/beyondjay/archive/2009/01/15/1376454.html
Asp. insert in the cache has two parameters do not understand
http://zhidao.baidu.com/question/9967841.html
Asp.net Cache Usage, single point of login
http://heisetoufa.javaeye.com/blog/315447
Asp. net cache solution
http://www.cnblogs.com/jeff377/archive/2008/08/28/1278989.html
Asp. net Cache Usage experience
http://www.xueit.com/html/2009-02/21_702_00.html
using the cache technology, to effectively improve ASP. net website performance
http://tech.it168.com/d/2008-01-14/200801142357414_1.shtml
details ASP. net status management cache application
http://www.xueit.com/html/2009-02/21_703_00.html

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.