默认的Null backing store 存储策略我们已经在上一章使用过了,Database Cache storage没研究透,先暂时不讲,如果有了解的朋友可以留言帮助完善,下面我们来看看Isolated storage 存储策略的实现:
< 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 Symmetric 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 >
< symmetricCryptoProviders >
< add type ="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.DpapiSymmetricCryptoProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
scope ="LocalMachine" name ="DPAPI Symmetric Crypto Provider" />
</ symmetricCryptoProviders >
</ securityCryptographyConfiguration >
< cachingConfiguration defaultCacheManager ="CacheManager" >
< cacheManagers >
< add name ="CacheManager" type ="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
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.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name ="NullBackingStore" />
</ backingStores >
< encryptionProviders >
< add name ="Symmetric Crypto Provider" type ="Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.SymmetricStorageEncryptionProvider, Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
symmetricInstance ="DPAPI Symmetric Crypto Provider" />
</ encryptionProviders >
</ cachingConfiguration >
</ configuration >
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
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
{
static void Main( string [] args)
{
//创建CacheManager
CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();
//添加缓存项
cacheManager.Add( "MyDataReader" , "123" );
//获取缓存项
string str = (String)cacheManager.GetData( "MyDataReader" );
//打印
Console.WriteLine(str);
}
}
}
运行结果: