The basic architecture of the caching Application Block is as follows. The figure clearly shows that the cache manager can cache data in the middle 3 mode:
1. null backing store Storage Policy: The default storage policy. The stored data is only stored in the memory cache and is not permanently saved. It can be used for all supported application types. it is suitable for saving some temporary data, or for saving some data that you do not want to save when you restart the program.
2. Isolated storage Storage Policy: The isolated storage policy applies to the following situations:
1. data needs to be stored persistently, with fewer users accessing it.
2. No database device.
3. Do not want to use resources with large overhead such as databases.
3. database cache storage policy: Database storage policy. The database can run on a server that uses the cache or that has the same applications on different servers. The number of applications is cached. The database can only rely on the Storage Limit of the database, it is troublesome to use, and you need to write some stored procedures by yourself.
You do not want to use resources with high overhead such as databases.
The default null backing store storage policy has been used in the previous chapter. database cache storage is not thoroughly studied. I will not talk about it for the time being. If you know it, you can leave a message to help you improve it, let's take a look at the implementation of the isolated storage policy:
1. Run entlibconfig.exe and select
BlocksMenu, click
Add cachingsettings.Click
Backing storesClick the plus sign in the upper right corner and select
Add backing stores, Click
Add isolatedstorage cache store.
2. If you want to encrypt the data in the cache, you can add an encryption object. You can click
Encryption providersClick the plus sign in the upper-right corner of the block and select
Add encryptionproviders, Click
Add encryption Ric crypto provider.For the sake of simplicity, we only make a simple key encryption object. For the encryption object, we will explain it in detail later.
Encryption ric cryptography providersClick the plus sign in the upper-right corner of the block and select
Add encryption riccryptography providers, Click
Add dpapi encryption Ric crypto provider. Add a simple key encryption object (for details about the enterprise database encryption module, click here ):
3. Click
FileMenu, click
Save, Save as
App. 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. Code
<Configuration>
<Configsections>
<Section name = "securitycryptographyconfiguration" type = "Microsoft. practices. enterpriselibrary. security. cryptography. configuration. cryptographysettings, Microsoft. practices. enterpriselibrary. security. cryptography, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" true "/>
<Section name = "cachingconfiguration" type = "Microsoft. practices. enterpriselibrary. caching. configuration. cachemanagersettings, Microsoft. practices. enterpriselibrary. caching, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" true "/>
</Configsections>
<Securitycryptographyconfiguration defaulthashinstance = "md5cng"
Defaultsymmetriccryptoinstance = "dpapi encryption Ric crypto provider">
<Hashproviders>
<Add name = "md5cng" type = "Microsoft. practices. enterpriselibrary. security. cryptography. hashalgorithmprovider, Microsoft. practices. enterpriselibrary. security. cryptography, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
Algorithmtype = "system. Security. cryptography. md5cng, system. Core, version = 3.5.0.0, culture = neutral, publickeytoken = b77a5c561934e089"
Saltenabled = "true"/>
</Hashproviders>
<Shortriccryptoproviders>
<Add type = "Microsoft. Practices. enterpriselibrary. Security. cryptography. Audit, Microsoft. Practices. enterpriselibrary. Security. cryptography, version = 5.0.414.0, culture = neutral, publickeytoken"
Scope = "localmachine" name = "dpapi encryption Ric crypto provider"/>
</Cipher riccryptoproviders>
</Securitycryptographyconfiguration>
<Cachingconfiguration defaultcachemanager = "cachemanager">
<Cachemanagers>
<Add name = "cachemanager" type = "Microsoft. Practices. enterpriselibrary. caching. cachemanager, Microsoft. Practices. caching. caching, version = 5.0.414.0, culture = neutral, publickeytoken = balance"
Expirationpollfrequencyinseconds = "60" maximumelementsincachebeforescavenging = "1000"
Numbertoremovewhenscavenging = "10" backingstorename = "nullbackingstore"/>
</Cachemanagers>
<Backingstores>
<Add name = "isolated storage cache store" type = "Microsoft. practices. enterpriselibrary. caching. backingstoreimplementations. isolatedstoragebackingstore, Microsoft. practices. enterpriselibrary. caching, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
Encryptionprovidername = "" partitionname = "isolated storage cache store"/>
<Add type = "Microsoft. Practices. enterpriselibrary. caching. Protocol. Protocol, Microsoft. Practices. enterpriselibrary. caching, version = 5.0.414.0, culture = neutral, publickeytoken = token"
Name = "nullbackingstore"/>
</Backingstores>
<Encryptionproviders>
<Add name = "Your Ric crypto provider" type = "Microsoft. practices. enterpriselibrary. caching. cryptography. symmetricstorageencryptionprovider, Microsoft. practices. enterpriselibrary. caching. cryptography, version = 5.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
Extends ricinstance = "dpapi extends Ric crypto provider"/>
</Encryptionproviders>
</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 is
Microsoft. Practices. enterpriselibrary. caching. dllAnd
Microsoft. Practices. enterpriselibrary. caching. cryptography. 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) 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: