I believe everyone knows the role of caching. Let's take a look at this article:ASP. NET micro Caching: benefits of a one-second Cache. In addition, we recommend that you read this article:Caching architecture guide for. NET Framework applications.
1. Lack of the Asp.net cache Function
In. NET Framework, the most frequently used cache is the cache service (system. Web. caching. cache) in Asp.net. It provides us with powerful caching functions, including output caching, fragment caching, and data caching (data caching is the main content discussed later ). However, this caching function has a major drawback: the cached content cannot be shared among multiple AppDomains.Appdomain Scope,Machine ScopeAndApplication farm ScopeOnly one appdomain scope level is implemented among the three levels. This is inconvenient for many applications.
For example, a website has an application for user management: Adding and editing user information, and other applications that need user information, such as BLOG, chat, and forums. These web applications run in different AppDomains. Because user information is infrequently changed and frequently accessed, it should be stored in the cache. However, because multiple AppDomains need to be accessed, it is difficult to use the cache function in Asp.net to ensure that user information can be immediately applied to other AppDomains after being changed. In the Application farm environment, information synchronization cannot be ensured.
2. cached applications
In each application, a large amount of data must be stored in the cache. The following describes how to use caching to obtain user information frequently used by each website. We declare an interface for the objects to be supported by the cache:
1 public interface icacheable
2 {
3 int cacheduration {Get ;}
4 int cachesliding {Get ;}
5 cachedependency dependency {Get ;}
6 cacheitempriority priority {Get ;}
7 // indicates when the item was added into the cache.
8 datetime timecached {Get; set ;}
9
10 // other members
11
12}
The user class implements the icacheable interface:
1 public class user: icachebale
2 {
3 private string username;
4 private datetime timecached;
5 Public const int maxonlineage = 5;
6
7 Public String Username
8 {
9 get {return username ;}
10 set {username = value ;}
11}
12
13 // other members of class user
14.
15
16 # region members of icacheable
17
18 public int cacheduration
19 {
20 get {return 5 ;}
21}
22
23 public int cachesliding
24 {
25 get {return 0 ;}
26}
27
28 public cachedependency dependency
29 {
30 get {return NULL ;}
31}
32
33 public cacheitempriority priority
34 {
35 get {return cacheitempriority. Normal ;}
36}
37
38 public datetime timecached
39 {
40 get {return timecached ;}
41 set {timecached = value ;}
42}
43
44 # endregion
45}
Users:
1 public class users
2 {
3 Public static user getuser (string username)
4 {
5 If (username = NULL | username. Length = 0 ){
6 throw new argumentnullexception ("username ")
7}
8
9 string cachekey = string. Format ("user-{0}", username. toupper ());
10
11 // step one
12 User user = cacheprovider. getcachedobject (cachekey) as user;
13
14 if (user = NULL |
15 user. timecreated <datetime. Now. addminutes (-user. maxonlineage )){
16 // step two
17 user = dataprovider. getuser (username );
18
19 // step three
20 if (user! = NULL ){
21 cacheprovider. insert (cachekey, user );
22}
23}
24
25 return user;
26}
27
28 // other members of class users
29.
30}
Note: Some programs store user objects that indicate user information in a hashtable object, and then add this hashtable object to the cache to avoid excessive keys in the cache. To make it simple, add the user's instance to the cache.
In the preceding users. getuser method, the main steps are as follows:
- Try to get the user instance from the cache:
User user = cacheprovider. getcachedobject (cachekey) as user;
- If the user instance cannot be found in the cache, or the instance has expired (the expiration here is different from the automatic expiration of the cache mechanism. The automatic expiration in the cache mechanism refers to the expiration when the dependent project changes or the specified storage time expires. Here, the user instance expires to update the user's online time, for example, to display all online users in a forum, you only need to query all online users in the database for a period later than the specified time (user. maxonlineage .), Obtains the user's instance from the database.
User = dataprovider. getuser (username );
- Finally, add the newly created user instance to the cache:
Cacheprovider. insert (cachekey, user );
3. General steps for coordinated use of cache and database
Now, we can sum up the above content: when getting data from the system, the main steps include:
- Obtain objects from the cache;
- If the object cannot be found, it will continue to be obtained from the database;
- Add the newly obtained object to the cache.
When adding/updating data to a database:
- Add/update data to a database
- Update the corresponding cache items.
4. Microsoft Enterprise Library-caching Application Block
As mentioned above, the cache in Asp.net is only implemented at the appdomain scope level. Does it implement machine Scope and application farm scope? The answer is yes in enterprise libarary (http://msdn.microsoft.com/library/default.asp? The Caching Application Block released in url =/library/en-US/dnpag2/html/entlib. asp implements three levels (however, I have never used it yet :)).
In earlier versions of the caching Application Block, memory map file was used to store cache objects. However, in the Enterprise Library released in January 2005, database and isolated storage were used to store cache objects. Memory Map Files are faster than database and isolated storage. Why not discard them? Is it because of security issues? Or?
The following article uses memory map file to cache data between AppDomains: devglobalcache-a way to cache and share data between processes
In addition, I think the Enterprise Library is complicated and not easy to use. Therefore, I am also starting to use the existing enterprise library to build a cache module that is easier to configure and simpler than the Enterprise Library.
5. Tips
- When obtaining the system. Web. caching. cache instance in a web application, use httpcontext. Current. cache with caution. Because httpcontext. Current only returns the correct instance when responding to the user request, otherwise the return is blank. For example, if a thread in the appdomain of a Web application is responsible for sending emails, httpcontext. Current should return an empty object in the thread. To obtain the correct cache object, use httpruntime. cache.
- For data that changes frequently and does not need to be cached, you should also put it in httpcontext. current. items to prevent multiple queries to the database for the same request.