Exercise2: Persistent Cache
This exercise demonstrates how to persistently cache data.
Step 1
OpenEmployeebrowser. slnProject, the default installation path should beC: \ Program Files \ Microsoft Enterprise Library January 2006 \ labs \ CS \ caching \ exercises \ ex02 \ beginAnd compile.
Step 2 implement offline Cache
1In solution manager, selectEmployeeservices. CSFile, selectView | codeMenu command and add the following namespace.
Using Microsoft. Practices. enterpriselibrary. caching. expirations;
2. GoGetcontactdetailsMethod, and add the followingCode.
Public Static Employeesdataset getcontactdetails ()
{
Employeesdataset dsemployees = Null ;
// Todo: Add persistent caching with time-out
// Attempt to retrieve from Cache
Cachemanager Cache = Cachefactory. getcachemanager ();
Dsemployees = (Employeesdataset) cache [cache_key];
// Retrieve from dataprovider 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 Dsemployees;
}
3. Modification MethodGetemployeephotoThe following code does not attempt to obtain information offline.
Public Static Bitmap getemployeephoto (guid employeeid)
{
Byte [] Photodata = Null ;
// Attempt to retrieve from Cache
Cachemanager 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= NewEmployeedataprovider ();
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 NewBitmap (MS );
}
}
Step 3 configure persistent Cache
1Select a project in solution manager.EnoughpiConfiguration FileApp. configFile, selectView | open...Menu command, selectEnterprise Library ConfigurationAnd clickOKButton.
2. SelectCaching Application Block | cache managers | cache managerNode, selectAction | new | isolated storageMenu command.
3. Set PropertiesPartitionnameIsEmployeebrowser.
PartitionnameMultiple caches are allowed to share the same physical storage location.
4. Save the applicationProgramConfiguration.
Step 4 run the application
1. SelectDebug | start without debuggingMenu command to run the application. Browsing a small amount of employee informationEmployeesLoad to the cache. Do not browse all employee information.
2In solution manager, selectConnectionmanager. CS, SelectView | codeMenu command, modify in the following codeIsonlinEAttribute Value.
Static Public Bool Isonline
{
Get {Return False;}
}
3. SelectDebug | start without debuggingMenu command to run the application. Currently, the application is offline and does not connect to the database.
4. Close the application andVisual Studio. NET.
MoreEnterprise LibraryOfArticleSee 《Enterprise LibrarySeries of articles"