ASP. NET cache Solution

Source: Internet
Author: User

ASP.. Net website applications are stateless. Every time the client executes the PostBack action, all objects need to be re-established. Of course, this execution mode is quite inefficient, so in ASP.. NET applications include three types of temporary data storage areas: application, session, and cache. The life cycle of an application is the same as that of an application. The life cycle of a session is the same as that of individual users. the life cycle of a cache is customized by the program designer. The display of a cache is more flexible than that of an application or session.
Selecting an appropriate cache solution can effectively improve the website's execution efficiency. The following three conditions can be set for cache Expiration.
1. Absolute Time: the time when the inserted object expires and is removed from the cache.
2. Floating time: the interval between the last time the object was accessed and the expiration time of the object. If the value is equal to 20 minutes, the object will expire and be removed from the cache after the last 20 minutes of access.
3. File dependency: the file or cache index key dependency of the project. For example, if you create a DataSet object based on the data in the XML file, you can add the dataset to the cache containing the cachedependency object. This object makes the dataset dependent on this XML file. If the XML file is changed, dataset is removed from the cache.
I often use cache to store the set data that is frequently accessed. The general practice is to create a "set category" based on the XML file, and then put this category into the cache, set the cache expiration condition to "floating time + XML file dependency 」. This cache solution has two advantages.
1. The more frequently this object is accessed, the higher the priority will be placed in the cache, and it is not easy to remove it from the cache.
2. When the set XML file content changes, the cache will be removed to keep the latest information.
 
The cache method is as follows:
Step 1: request object.
Step 2: Check whether the object exists in the cache.
Step 3: If this object exists in the cache, the object is obtained from the program. Otherwise, execute step 4.
Step 4: Create an object from the XML file, store the object in the cache, and return the object.
 
Assuming that the tmysetting object uses the above cache scheme, the program code is implemented as follows. For the tbserializerutil. xmlfiletoobject Method for deserialization in the program code, see the article "Object serialization function library.

 

''' <Summary>
''' Get the tmysetting object based on the key value.
''' </Summary>
''' <Param name = "key"> key value. </Param>
Public Function getmysetting (byval key as string) as tmyseting
Dim scachekey as string
Dim ocache as system. Web. caching. Cache
Dim omyseting as tmyseting
Dim sfilename as string

'The cache key value is type name. Key value 」
Scachekey = string. Format ("{0}. {1}", GetType (tmyseting). Name, key)

'Asp. Net cache objects
Ocache = system. Web. httpcontext. Current. Cache

If ocache (scachekey) isnot nothing then
'Obtain this object directly from the cache.
Omyseting = ctype (ocache (scachekey), tmyseting)
Else
'Create an object from an XML file
Sfilename = "Object Name"
Omyseting = ctype (tbserializerutil. xmlfiletoobject (sfilename, GetType (tmyseting), tmyseting)

'Save the object to the cache and set the cache removal time
'1. File dependency
'2. The floating time is 5 minutes.
Ocache. insert (scachekey, omyseting ,_
New system. Web. caching. cachedependency (sfilename ),_
System. Web. caching. cache. noabsoluteexpiration, new timespan (0, 5, 0 ))
End if
Return omyseting
End Function

 

Postscript:
Enterpriselibrary also has a cachemanager object that processes the cache, which is similar to the cache object of ASP. NET. If you want to write a three-tier architecture, you can also use enterpriselibrary's cachemanager to replace ASP. NET cache. Later, we will introduce cachiselibrary's cachemanager object.

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.