Enterprise Library-caching Application Block learning Manual (Latest Version) Part 2
Source: Internet
Author: User
This article demonstrates how to use the Enterprise Library-caching Application Block module in different applications. The following describes the use of persistent backend storage (cache) and offline cache expiration policies. This article by the http://blog.entlib.com open source ASP. Net blog platform team according to the entlib Hol manual compilation to provide, welcome to exchange.
Exercise 2: Persistent CacheFirst open the employeebrowser. sln project file under the ex02 \ begin directory.
Implement offline Cache1. open employeeservice in the project. add the following namespace reference to the CS code file: using Microsoft. practices. enterpriselibrary. caching. expirations; 2. find the getcontactdetails method in the Code and add the following code: public static employeesdataset getcontactdetails () {employeesdataset dsemployees = NULL; // attempt to retrieve from cache icachemanager cache = cachefactory. getcachemanager (); dsemployees = (employeesdataset) cache [cache_key]; // retrieve from dataprovid Er if not in cache and online if (dsemployees = NULL & connectionmanager. isonline) {employeedataprovider dataprovider = new employeedataprovider (); dsemployees = dataprovider. getemployees (); // expire in 2 days absolutetime expiry = new absolutetime (New timespan (2, 0, 0, 0); cache. add (cache_key, dsemployees, cacheitempriority. high, null, new icacheitemexpiration [] {expiry});} return dsemplo Yees;} the above Code connects to the database when the application is online. The returned DataSet object is added to the cache, And the cache priority and cache expiration Policy are specified using the overload add method. In this case, the employee data retained on the local machine by the user cannot exceed two days, which can reduce the possibility that the employee may disclose the information to other companies, because the data will be deleted if the cache expires. 3. Modify the getemployeephoto method. When the application is offline, do not connect to the database to retrieve employee information. Public static bitmap getemployeephoto (guid employeeid) {byte [] photodata = NULL; // attempt to retrieve from cache icachemanager cache = cachefactory. getcachemanager (); photodata = (byte []) cache [employeeid. tostring ()]; // todo: retrieve from dataprovider if not in cache and online if (photodata = NULL
& Connectionmanager. isonline) {Employeedataprovider dataprovider = new employeedataprovider (); photodata = dataprovider. getemployeephotodata (employeeid); cache. add (employeeid. tostring (), photodata);} // No data found. if (photodata = NULL) return NULL; // convert bytes to Bitmap Using (memorystream MS = new memorystream (photodata) {return New Bitmap (MS );}}
Configure persistent Cache1. Use the configuration management tool of Enterprise Library to open the app. config configuration file. 2. Select the caching Application Block | cache manager | cache Manager node, and then select the new | isolated storage menu item.
3. Select the isolated storage node and set the partitionname attribute to employeebrowser. The partition name allows multiple caches to use isolated storage without interfering with each other. Save the preceding configuration file.
Run the sample program1. browsing some employees will cache the employee's image information, but do not browse all employee information. If the employee is not browsed, the image will not be cached (to facilitate the next comparison), and then close the application. 2. Open the connectionmanager. CS code file in the project and modify the isonline attribute, as shown in the following figure. The simulated application is offline. Static public bool isonline {get {return false;} under normal circumstances, this class is responsible for detecting the connection status of the server, determining whether the client is online, and connecting to the database. 3. Run the sample program again. The application is offline and cannot access the database. Now, you need to obtain employee contact information from the cache, including employee pictures. However, the cache only contains the pictures of employees that have been browsed online. For employee information that has not been browsed, only employee information is displayed, and employee image information cannot be displayed, as shown in.
Http://www.entlib.com professional ASP. NET e-commerce platform team, welcome to continue to visit the caching Application Block learning manual.
Reference:Caching Application Block hands-on labs for Enterprise Library
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.