Enterprise Library 2.0 hands on lab translation (10): cache application block (2)

Source: Internet
Author: User

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"

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.