Cache application using Microsoft Application Block

Source: Internet
Author: User

You can download the caching Application Block from http://www.microsoft.com/resources/practices/default.mspx.

Here we will introduce the simple usage

1. Create a Windows form TestProgram

Textbox is used to display cache content, and button is used to obtain cache items. The program cannot be simple.

2. Add a cache reference (copy it directly in the block installation directory ):

Microsoft. applicationblocks. cache. dll
Microsoft. applicationblocks. Data. dll
Microsoft. applicationblocks. exceptionmanagement. dll
Microsoft. applicationblocks. exceptionmanagement. Interfaces. dll
Memorymappedfilestream. dll

3. Configure the config file

Because it is win form, an app. config is added. If it is a Web reference, it corresponds to the Web. config file.

Default file example: C: \ Program Files \ Microsoft application blocks for. Net \ caching framework \ code \ CS \ solution items

Copy the content of APP. config directly.

All right, all deployment is complete. Let's take a look at the code below.

 

Using System. Data;
Using Microsoft. applicationblocks. cache;

Namespace Winapp_block_cache
{
/**/ /// <Summary>
///A summary of form1.
/// </Summary>
Public   Class Form1: system. Windows. Forms. Form
{
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. textbox textbox1;

Private   Void Button#click ( Object Sender, system. eventargs E)
{
This. Textbox1.text=(String) Getcacheditem ("Hello");
}
Private   Object Getcacheditem ( String Itemname)
{
// Get singlton of Cache Management
Cachemanager cachemgr = Cachemanager. getcachemanager ();
Object OBJ = Cachemgr. getdata (itemname );
If (OBJ =   Null )
{
// The following describes how to set extendedformattime.
/**/ ///   <Remarks>
/// Extended format sintax: <Br/>
///  
/// Minute-0-59 <Br/>
/// Hour-0-23 <Br/>
/// Day of month-1-31 <Br/>
/// Month-1-12 <Br/>
/// Day of week-0-7 (Sunday is 0 or 7) <Br/>
/// Wildcards-* means run every <Br/>
/// Examples: <Br/>
/// * ***-Expire every minute of every day <Br/>
/// 5 ***-Expire 5th minute of every hour or every minute
/// After 1 hour from the last used time, whichever
/// Is earlier <Br/>
/// * 21 ***-expire every minute of the 21st hour of every day
/// Or one hour after every day <Br/>
/// 31 15 ***-Expire pm every day <Br/>
/// 7 4 ** 6-Expire Saturday AM <Br/>
/// 15 21 4 7 *-Expire 9: 15 pm on 4 July <Br/>
///   </Remarks>
Microsoft. applicationblocks. cache. icacheitemexpiration [] exps =  
{
// Set cache expiration dependency. Here, it is set to expire in 1 minute.
// You can set multiple expiration dependencies. This block provides the following four types of expiration dependencies:
// Absolutetime
// Extendedformattime
// Filedependency
// Slidingtime

New Microsoft. applicationblocks. cache. expirations. extendedformattime ( " ***** " )
} ;
OBJ =   " Hello world !! "   + Datetime. Now. millisecond. tostring ();

// If you do not want to process cache expiration events, set callback to null.
// Here we use cacheexpiredprocess to process cache expiration.
Cachemgr. Add (itemname,
OBJ,
Exps,
Cacheitempriority. High,
New Cacheitemremovedcallback (cacheexpiredprocess ));
}
Return OBJ;
}
// Set a function to process expired events.
Private   Void Cacheexpiredprocess ( String Cachekey, cacheitemremovecause cause)
{
MessageBox. Show (cachekey+ "Is expired!");
}

}
}

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.