Microsoft Enterprise Library 5.0 series (1): Caching Application Block (beginner)

Source: Internet
Author: User

For more information about this article, see the following link: http://msdn.microsoft.com/en-us/library/ff664753%28v=PandP.50%29.aspx

MicrosoftEnterprise Library 5.0: http://www.microsoft.com/downloads/details.aspx? FamilyId = bcb166f7-dd16-448b-a152-9845760d9b4c & displaylang = en

MicrosoftEnterprise Library 5.0 Documentation: http://entlib.codeplex.com/releases/view/43135

The enterprise database cache application module has the following features:

  • You can use the graphical interface to configure and set the Enterprise Library cache application module.
  • You can configure a persistent storage unit, or use an independent storage or enterprise database data to access the application module. Its status is synchronized with the cache in the memory.
  • The administrator can manage the configuration and use the group policy tool.
  • You can create custom extended expiration policies and storage unit modules.
  • Thread security can be ensured.

The following describes how to use the cache application module in Microsoft Enterprise Library 5.0.

1. Download and install MicrosoftEnterprise Library 5.0, and then runEntLibConfig.exe

2. SelectBlocksMenu, clickAdd CachingSettings.

Configuration attributes:

3. ClickFileMenu, clickSave, Save asApp. configFile, you can save it to the desktop first, and then use it. Use notepad to open App. config, you can see the following content.

<Configuration>

<ConfigSections>

<Sectionname = "cachingConfiguration" type = "Microsoft. practices. enterpriseLibrary. caching. configuration. cacheManagerSettings, Microsoft. practices. enterpriseLibrary. caching, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "requirePermission =" true "/>
</ConfigSections>
<CachingConfigurationdefaultCacheManager = "Cache Manager">
<CacheManagers>
<Addname = "Cache Manager" type = "Microsoft. Practices. EnterpriseLibrary. Caching. CacheManager, Microsoft. Practices. Caching. Caching, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = token"
ExpirationPollFrequencyInSeconds = "60" maximumElementsInCacheBeforeScavenging = "1000"
NumberToRemoveWhenScavenging = "10" backingStoreName = "NullBackingStore"/>
</CacheManagers>
<BackingStores>
<Addtype = "Microsoft. Practices. EnterpriseLibrary. Caching. Protocol. Protocol, Microsoft. Practices. EnterpriseLibrary. Caching, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = token"
Name = "NullBackingStore"/>
</BackingStores>
</CachingConfiguration>
</Configuration>

4. next, you can create an application to use the configured cache application module. Here, I created a console application named test and saved the App. copy the config file to the project folder:

 

5. To use the cache application module, You need to import the corresponding Dll file. What we want to import here isMicrosoft. Practices. EnterpriseLibrary. Caching. dllAdd the App. config file to the project and add Microsoft. Practices. EnterpriseLibrary. Caching and using Microsoft. Practices. EnterpriseLibrary. Caching. Expirations references:

 

Add reference:

Using Microsoft. Practices. EnterpriseLibrary. Caching;
Using Microsoft. Practices. EnterpriseLibrary. Caching. Expirations;

6. Add and read cache items. The following example shows how to save a string object to the cache. In actual applications, we often use it to store read data from the database.DataSetNote:

(1) The data to be read must be converted to the type you need.

(2) check whether the value is null during reading.

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Using Microsoft. Practices. EnterpriseLibrary. Caching;
Using Microsoft. Practices. EnterpriseLibrary. Caching. Expirations;

Namespace test
{
Class Program
{
Staticvoid Main (string [] args)
{
// Create a CacheManager
CacheManager cacheManager = (CacheManager) CacheFactory. GetCacheManager ();

// Add a cache entry
CacheManager. Add ("MyDataReader", "123 ");

// Obtain cache items
String str = (String) cacheManager. GetData ("MyDataReader ");

// Print
Console. WriteLine (str );
}
}
}

Running result:

7. Remove the project.

// Remove cache items
CacheManager. Remove ("MyDataReader ");

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.