Developing Applications Using the Caching Application Block

Enterprise Library 4.1 - October 2008

Developing Applications Using the Caching Application Block

用高速缓存应用块开发应用程序

This section describes how to use the Caching Application Block to develop applications. It explains how to enter configuration information for the application block, incorporate it into your solution, and select a backing store. This section includes the following topics:

此章节讲述怎样使用高速缓存应用块开发应用程序。它阐述了怎么样为应用程序块来配置信息,合并到你的解决方案中,选择一个后台存储。这章包括下面主题:

输入配置信息

加入程序代码

选择一个后台存储

All application blocks ship as binary assemblies and as source code. If you want to use the source code, you must compile it before you can use the QuickStarts and configuration tools. To learn how to compile the Enterprise Library source code, see Building Enterprise Library from the Source Code.

所有的应用程序块都是二进制编码也都是源代码。如果你需要用这个源代码,在你用quickstart和配置工具之前你必须编译它。学习如何编译企业库源代码,请看Building Enterprise Library from the Source Code

Enterprise Library 4.1 - October 2008

Entering Configuration Information

录入配置信息

These procedures explain how to configure the Caching Application Block. Properties associated with the nodes appear in the right pane of the Configuration Console or in the Properties window of the Visual Studio Configuration Editor.

这些程序解决了怎么样配置高速缓存应用块。属性合并到配置控制台的右面的面板内的结点中或者存在于vs编辑器的属性窗口中。

If you are going to use the Data Access Application Block as the backing store, you must configure that application block before you configure the Caching Application Block.

如果你打算用数据访问程序块作为后台存储。你必须配置它在你配置高速缓存应用程序块之前。

For details of the schema for the Caching Application Block configuration, see Source Schema for the Caching Application Block.

高速缓存应用程序块配置的计划细节,请看Source Schema for the Caching Application Block.

To add the Caching Application Block

加入高速缓存应用块

1.       Open the configuration file. For more information, see Configuring Enterprise Library.

1.打开配置文件。更多信息,请参见Configuring Enterprise Library.

2.       Right-click Application Configuration, point to New, and then click Caching Application Block.

2.右击程序配置,点击新建,然后点击高速缓存应用块。

3.       The configuration tool automatically adds the Cache Manager node with default settings.

3.配置工作自动的加入cache manager节点伴随着一些默认设置。

To configure the cache manager

配置cache manager

1.       Click the Caching Application Block node.

1.点击高速缓存应用块节点。

2.       (Optional) Change the DefaultCacheManager property name. The default cache manager is used if the code does not specify a cache manager. Either enter a new name or select one from the drop-down list. The default name is CacheManager.

2.(可选的)改变DefaultCacheManager属性名称。如果代码没有指定一个cache manager的话,默认的cache manager将被使用。或者输入一个新名称或从下拉框中选择一个。默认的名称是cachemanager.

3.       Click the CacheManager node (if you renamed the cache manager, the node will have the name you assigned it).

3.点击cachemanager节点(如果你重命名了cache manger,那个结点将出现你指定的名字)。

4.       (Optional) Set the ExpirationPollFrequencyInSeconds property. This is the frequency of the timer that regulates how often the background scheduler checks for expired items. The unit is seconds; the minimum time is 1 second, and the default setting is 60 seconds.

4.(可选的)设置ExpirationPollFrequencyInSeconds属性。这是个定时器频率,它管理多久时间后台计划检查终止条目。单位是秒。最小时间是1秒,默认时间是60秒。

5.       Set the MaximumElementsInCacheBeforeScavenging property. This is the maximum number of elements that can be in the cache before scavenging. The default setting is 1000 elements.

5.设置MaximumElementsInCacheBeforeScavenging属性。在高速缓存注销之前这是元素的最大数。默认设置是1000个元素。

6.       (Optional) Rename the CacheManager node. The default name is CacheManager.

6.(可选的)重命名cacheManager节点。

7.       Set the NumberToRemoveWhenScavenging property. This is the number of elements to remove after scavenging begins. The default setting is 10 elements.

7.设置NumberToRemoveWhenScavenging属性。这是注销开始之后要移除的元素数目。默认设置是10个元素。

By default, the cache stores items only in memory and assigns the value of the backing store to NullBackingStore. You can also configure the Caching Application Block to use database cache storage, isolated storage, or custom cache storage. Database cache storage uses the Data Access Application Block.

另外,高速缓存存储条目仅在内存当中,指派后台存储数值到NullBackingStore中。你也能配置高速缓存应用块使用高速缓存存储,孤立存储或自定义缓存存储。数据库高速缓存存储使用数据访问应用程序块。

To configure the Caching Application Block for database cache storage

为数据库高速缓存存储配置高速缓存应用程序块

1.       Right-click CacheManager (if you renamed the cache manager, the node will have the name you assigned it), point to New, and then click DatabaseCacheStorage.

1.右击CacheManager(如果你重命名高速缓存管理器,此节点将是你指定的名称),点击新建,然后点击databaseCacheStorage.

2.       The configuration tool automatically adds the Data Access Application Block. For information about configuring this application block, see The Data Access Application Block documentation.

2.配置工具自动加入数据访问应用程序块。关于配置这个应用块的更多信息,请参见The Data Access Application Block文档。

3.       Click the DataCacheStorage node.

3.点击DataCacheStorage结点。

4.       Set the DatabaseInstance property. This is the name of the database connection string. It must correspond to the name of a connection string in the Data Access Application Block configuration. Either enter the name or select it from the drop-down list.

4.设置DatabaseInstance属性。这个数据库连接字符串。它必须和数据访问应用程序块的连接字符串的名称一致。或者输入名称或者从下拉框中进行选择。

5.       (Optional) Set the Name property by renaming the DataCacheStorage node.

5.(可选的)设置Name属性通过重命名DataCacheSTorage结点。

6.       Set the PartitionName property. This identifies the portion of the database that the cache manager will use.

6.设置PartitionName属性。这个属性确定着一部分高速缓存数据库将用到的数据库。

To configure the Caching Application Block for isolated storage

为独立存储配置高速缓存应用块

1.       Right-click CacheManager (if you renamed the cache manager, the node will have the name you assigned it), point to New, and then click IsolatedStorage.

1 右击Cachemanger(如果你重命名那个缓存管理器,那个结点将是你指定的名称),点击新建,然后点击IsolatedStorage.

2.       If you want to encrypt the information stored in isolated storage, right-click Isolated Storage, point to New, and click Symmetric Storage Encryption. The configuration tool automatically adds the Cryptography Application Block. For information about configuring this application block, see The Cryptography Application Block documentation.

2.如果你需要在独立存储中加密信息存储,右击Isolated Storage,点击新建,点击Symmetric Storage Encryption.配置工具会自动地加入加密应用程序块。关于加密应用块的更多信息,请参见The Cryptography Application Block文档。

3.       (Optional) Set the Name property by renaming the IsolatedStorage node.

3.(可选的)通过重命名IsolatedStorage结点设置Name属性。

4.       Set the PartitionName property. This identifies the portion of isolated storage that the cache manager will use.

4.设置PartitionName属性。这个属性确认着一部分高速缓存管理将用到的独立存储。

To configure the Caching Application Block for custom cache storage

为自定义缓存存储配置高速缓存应用块

1.       Right-click CacheManager (if you renamed the cache manager, the node will have the name you assigned it), point to New, and then click Custom CacheStorage.

1.右击CacheManger(如果你重命名高速缓存管理器,那个结点将显示你指定的名称)。点击新建,然后点击Custom CacheStorage.

2.       In the Attributes property section of the right pane, click the ellipsis button (...).

2.在右面面板的Attributes属性项中,点击省略号(.

3.       In the EditableKeyValueCollectionEditor dialog box, click Add to add a new name/value pair.

3.EditableKeyValueCollectionEditor对话框中,点击Add加入一个新的名称/数据对。

4.       In the right pane of the EditableKeyValueCollectionEditor dialog box, enter the key name and the value of the property.

4.在EditableKeyValueCollectionEditor对话框的右面面板中,输入关键名和属性值。

5.       Add more name/value pairs as appropriate, and then click OK.

5.同样适当的加入更多的名称/值,然后点击OK

6.       (Optional) In the Name section of the configuration properties, change the name of the custom cache storage. The default name is CacheStorage.

6.(可选的)在配置属性的Name项中,改变自定义缓存存储的名称,默认名称是CacheStorage.

7.       In the Type property section of the right pane, click the ellipsis button. To filter the list, in the Filter edit box type the string to use to filter the list, for example type "string" to filter for all classes containing the word "string". If the type you want is not included in the Assemblies folder, click Load from File or Load from GAC on the TypeSelector to find the assembly that contains the type you want.

7.在右面面板的Type属性项,点击ellipsis按钮。可以筛选列表,在筛选编辑框中指定字符串来使用筛选列表,例如敲入”string”,则筛选所有包含”string”字符的所有类。如果那个类型你需要不包含在源目录中,点击Load从文件中或者点击Load from GACTypeSelector上以找到你要的包含那个类型的源文件。

If you want to add another instance of the cache manager, right-click the CacheManagers node, point to New, and then click CacheManager. Repeat the preceding procedures. There can be only one default cache manager. Each instance of the cache manager must have a unique name.

如果你需要在缓存管理器中加入另外一个实例,右击CacheManager结点,指向New,然后点击CacheManager.重复前述的步骤。那就仅仅一个默认的缓存管理器。每个缓存管理器的实例必须有一个唯一的名称。

 Usage Notes

使用后记

The configuration settings for the Caching Application Block should reflect both an application's caching usage pattern and its system environment, such as the amount of available memory. For example, if an application adds items to the cache at a greater rate than it removes them when scavenging (this is a configurable setting), the cache will continue grow. Over time, this can cause memory starvation. Use the application block's performance counters to help tune the configuration settings for each application.

高速缓存应用块的配置设置应当反映应用程序的缓存使用方法和它的系统环境。例如可用内存的数量。例如,如果程序加载一个项目到缓存中比当它注销时移除时(这是一个配置设置)速度更快时,缓存将一直增长。那么随着时间的延长,这肯定会引发内存不足,使用应用程序块的性能计数器能帮助为每一个程序调较配置设置。

Enterprise Library 4.1 - October 2008

Source Schema for the Caching Application Block

高速缓存应用块的源模型

This topic lists the XML elements and attributes used to configure the Caching Application Block. You can manually edit the XML data, but the Enterprise Library configuration tools greatly simplify this task. If you choose to manually edit the XML, use the schema information contained in this topic.

这个主题列举了XML元素和属性怎么用来配置缓存应用块。你能手工编辑XML数据,但是企业库配置工作能大大简化这项任务。如果你选择手工编辑XML,使用此章节包含的模型信息。

The configuration file has the following section handler declaration.

配置文件包含着下面的节点标识声明。

XML

Copy Code

<configSections>

<section name="cachingConfiguration"

         type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings,

               Microsoft.Practices.EnterpriseLibrary.Caching,

               Version= 4.1.0 .0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</configSections>

The section handler declaration contains the name of the configuration settings section and the name of the section handler class that processes configuration data in that section. The name of the configuration settings section is cachingConfiguration. The name of the section handler class is Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings.

这个节点标识声明包含着配置设置节点的名称和节点标识类的名称(它处理配置数据)。配置设置节点的名称是cachingConfiguration.那个节点标识类的名称是Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings.

 cachingConfiguration Element

cachingConfiguration元素

The cachingConfiguration element specifies the configuration of a Caching Application Block. This element is required.

cachingConfiguration元素指定了高速缓存应用块的配置。这个元素是必须的。

Attributes and Child Elements

属性和子元素

The following sections describe attributes and child elements of the cachingConfiguration element.

下面的部分描述了cachingconfiguration元素的属性和子元素。

Attributes

属性

The following table lists the attributes for the cachingConfiguration element.

下面的表格列举了cachingConfiguration元素的属性。

Attribute

Description

defaultCacheManage

 

The Caching Application Block uses this cache manager if the application code does not provide a named instance of a cache manager as a parameter to the CacheFactory when it creates a cache manager. This attribute is required.

当建立缓存管理器的时候,缓存应用块使用这个缓存管理器如果应用程序没有指定一个命名了的缓存管理器的实例作为参数传递给CacheFactory的话。这个参数是必须的。

 encryptionProviders Element

encryptionProviders元素

The encryptionProvider element is a child element of the cachingConfiguration element. The encryptionProvider element lists the encryption providers that the backing stores can use. This element is optional.

encryptionPorivder元素是一个cachingConfiguration元素的子元素。encryptionProvider元素列出了后台存储能够使用的加密提供者。这个元素是可选的。

add Child Element

add子元素

The add element is a child element of the encryptionProviders element. The add element adds the name of an encryption provider. This element is optional. There can be multiple add elements.

Add元素是一个encryptionProviders元素的子元素。Add元素添加加密提供者的名称。这个元素是可选的。允许多个add元素。

Attributes

属性

The following table lists the attributes for the add element.

下面的表列出了add元素的属性。

Attribute

Description

name

The name of the encryption provider. The name must be unique within the section. This attribute is required.

加密提供者的名称。在节点中这个名称必须是唯一的。这个元素是必须的。

Type

The type name of a class that implements the IStorageEncryptionProvider interface. This attribute is required.

类的类别名称实现了IStorageEncryptionProvider接口。这个属性是必须的。

 backingStores Element

backingStroes元素

The backingStores element is a child element of the cachingConfiguration element. The backingStores element lists the backing stores that the cache manager can use. If you do not specify a backing store during configuration, the block uses only the in-memory backing store. This element is required.

backingStores元素是一个cachingconfiguration元素的子元素。backingStores元素列举了高速缓存管理器能够使用的后台存储。如果在配置过程中你不指定一个后台存储,那个应用块仅仅可能使用在内存中的后台存储中。这个元素是必须的。

add Child Element

add子元素

The add element is a child element of the backingStores element. The add element adds the name of a backing store. This element is required. There can be multiple add elements.

Add元素一个backingStores元素的子元素。Add元素加一个后台存储的名称。这个元素是必须的。可以加入多个add元素。

Attributes

属性

The following table lists the attributes for the add element.

下面的表格列举了add元素的属性。

Attribute

Description

name

The name of the backing store. This attribute is required.

后台存储的名称。这个属性是可选的。

Type

The type name of a class that derives from the BaseBackingStore class. This attribute is required.

这个类的类别名称起源于baseBackingStore类。这个属性是必须的。

encryptionProviderName

The name of an encryption provider. The name must be unique within the section. This attribute is optional.

加密提供者的名称。在节点内部这个名称必须是唯一的。这个属性是可选的。

partitionName

If you are using the database cache store, this attribute identifies the section or portion of the database that the cache manager will use. If you are using isolated storage, it identifies the section or portion of the isolated storage that the cache manager will use. This attribute is required.

如果你使用数据库缓存存储,这个属性标识节或者缓存管理器将使用的部分数据库。如果你使用独立存储,它标志着节或缓存管理器将使用的独立存储。这个属性是必须的。

databaseInstanceName

The name of the database instance. This attribute is required for the database backing store.

数据库实例的名称。这个属性是数据库后台存储所必须的。

 

 cacheManagers Element

cacheManagers元素

The cacheManagers element is a child element of the cachingConfiguration element. The cacheManagers element lists the cache managers. Cache managers represent instances of caches that can be used in the application. This element is required.

cacheMangers元素是cachingconfiguration元素的子元素。Cachemangers元素列举了缓存管理器。缓存管理器描述着能被应用程序所使用的缓存实例。这个元素是必须的。

add Child Element

add子结点

The add element is a child element of the cacheManagers element. The add element adds the name of a cache manager. This element is optional. There can be multiple add elements.

Add元素是一个cacheManagers元素的子元素。Add元素加入缓存管理器的名称。这个元素是可选的。可以加入多个add元素。

Attributes

属性

The following table lists the attributes for the add element.

下面的表格列举了add元素的属性

Attribute

Description

name

The name of the cache manager. The name must be unique within the section. This attribute is required.

缓存管理器的名称。在节点内这个名称必须是唯一的。这个属性是必须的。

expirationPollFrequencyInSeconds

Sets the timer that regulates how often the background scheduler checks for expired items. This attribute must be a positive integer. This attribute is required.

设置定时器控制多长时间后台计划检查终止条目。这个属性必须是一个正整数。这个属性是必须的。

maximumElementsInCacheBeforeScavenging

Sets the maximum number of items that can be in the cache before scavenging begins. This attribute must be a positive integer. This attribute is required.

设置在开始清除之前在缓存中的最大条目的数量。这个属性必须是一个正整数。这个属性是必须的。

numberToRemoveWhenScavenging

Sets the number of items to remove when scavenging begins. This attribute must be a positive integer. This attribute is required.

当清除开始时设置移除的条目数量。这个属性必须是一个正整数。这个属性是必须的。

backingStoreName

The name of the backing store to use with this cache manager. This attribute is required.

后台存储的名称和缓存管理器一起使用。这个属性是必须的。

 

Enterprise Library 4.1 - October 2008

Adding Application Code

加入应用程序代码

The Caching Application Block is designed to support the most common situations for storing data in a cache. When adding your application code, refer to the scenarios in the Key Scenarios sections and select the ones that best suit your situation. Use the code that accompanies the scenario either as it is or adapt it as needed.

高速缓存应用块被设计成支持大多数通用场景中在缓存中存储数据。当加载你的应用程序代码的时候,参考Key Scenarios节的场景,选择最适合的你的场景。使用那些场景中的代码或者直接使用或者修改成适合需要的。

To prepare your application

准备你的应用程序

1.       Add a reference to the Caching Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Caching.dll assembly. Select the assembly, and then click OK to add the reference.

1.加入参考到缓存应用控制块元中,在visual studio中,右击你的项目结点在解决方案窗口中,然后点击加入参考,点击修改标号键找到Microsoft.Practices.EnterpriseLibrary.Caching.dll元位置。选择并加载它。

2.       Use the same procedure to set a reference to the Enterprise Library Common assembly, named Microsoft.Practices.EnterpriseLibrary.Common.dll.

2.使用相同的过程设置一个参考加载Microsoft.Practices.EnterpriseLibrary.Common.dll

3.       Follow the same procedure to set a reference to the Enterprise Library Common assembly, Microsoft.Practices.EnterpriseLibrary.Common.dll and to the ObjectBuilder assembly, Microsoft.Practices.EnterpriseLibrary.ObjectBuilder2.dll.

3.依照同样的步骤设置参考Microsoft.Practices.EnterpriseLibrary.Common.dllMicrosoft.Practices.EnterpriseLibrary.ObjectBuilder2.dll

4.       If you are using the database backing store, add a reference to Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll and Microsoft.Practices.EnterpriseLibrary.Data.dll.

4.如果你使用数据库后台存储,加载Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll and Microsoft.Practices.EnterpriseLibrary.Data.dll.

5.       If you are using the Cryptography Application Block to encrypt data in the cache, add references to Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll and Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll.

5.如果你在缓存中使用加密应用块加密数据,加入参考Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll and Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll.

6.       (Optional) To use elements from the Caching Application Block without fully qualifying the element reference, add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file.

6.(可选的)没有引用完整的元素参考的情况下在缓存应用块中使用这些元素,在你源代码文件的顶部加入下面的UsingImportsVB中)声明。

C#

Copy Code

using Microsoft.Practices.EnterpriseLibrary.Caching;

using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

Visual Basic

Copy Code

Imports Microsoft.Practices.EnterpriseLibrary.Caching

Imports Microsoft.Practices.EnterpriseLibrary.Caching.Expirations

 

Note:

For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.

对于VB工程,你也可以使用项目设计器的参考页管理参考和导入命名空间。访问这些参考页,在解决方案管理器中选择一个项目结点,然后点击PropertiesProject菜单上。当项目设计器出现的时候,点击References标签。

 

Next, add the application code. Generally, there are two steps to create code that uses the Caching Application Block:

然后,加入应用代码。通常,使用缓存应用块有两个步骤建立代码。

1.       Create the CacheManager object.

1.建立CacheManager对象。

2.       Call the appropriate method.

2.调用适当的方法。

Each key scenario demonstrates how to incorporate these steps into an application.

每个关键场景模拟如何合并这些步骤到一个应用程序中。

Enterprise Library 4.1 - October 2008

Selecting a Backing Store

选择一个后台存储

Each cache manager can be configured to store data only in memory, which means that it uses the null backing store, or each cache manager can be configured to store data both in memory and in persistent storage. The type of persistent storage is specified when you configure the backing store. Backing stores let cached data survive if the application must be restarted. In its original state, the Caching Application Block supports two types of persistent backing stores, each of which is suited to particular situations:

每一个缓存管理器仅仅在内存中能被配置可以存储数据,意味着它用null backing store,或者每个缓存管理器能被配置存储在内存和持久存储中的数据。当你配置后台存储的时候持久层的种类被配置。如果应用程序必须被重启,后台存储允许缓存数据再生。在初始状态,高速缓存应用块支持持久后台存储的两种类型,每一种适合特定的场合。

If you intend to perform caching in a multiple-server environment, such as a Web farm, see Considerations for Server Scenarios.

如果你打算执行缓存在多服务的环境中,例如web form,请看Considerations for Server Scenarios.

Developers can extend the Caching Application Block to support additional types of backing stores. For more information about this topic, see Extending and Modifying the Caching Application Block.

开发者能够扩展高速缓存应用块支持后台存储的额外类型。关于这个主题的更多细节,请看Extending and Modifying the Caching Application Block.

Note:

An application can use more than one cache; each cache will be represented by a cache manager in the application's configuration. The Caching Application Block does not support the use of the same persistent backing store location by multiple cache managers in an application. However, multiple cache managers in an application can have the same partition name.

一个程序能够使用多于一个缓存。每一个缓存将被描述通过应用程序的配置中的缓存管理器。高速缓存管理器不支持应用程序的缓存管理器使用相同位置的持久后台存储。然而,应用程序中的多个高速缓存管理器能有相同的隔离名称。

 Using the Null Backing Store

使用空后台存储

The null backing store is the default option when you configure the Caching Application Block. It does not persist cached items. This means that cached data exists in memory only; it does not exist in persistent storage. The null backing store is suitable for situations where you want to refresh cached items from the original data source when the application restarts. It can be used with all the supported application types. For a list of these types, see Introduction to the Caching Application Block.

当你配置高速缓存管理器的时候,空后台存储是默认的选择。它没有缓存条目。这意味着缓存数据仅仅存在于内存中。它不存在于持久存储中。空后台存储适合于当程序重启时从原始数据源刷新缓存条目的场合。它能和所有的支持的程序类型一起使用。这些类型的列表,请看Introduction to the Caching Application Block

 Using the Isolated Storage Backing Store

使用独立后台存储

Isolated storage is appropriate in the following situations:

独立存储适合于下面的场景:

  • Persistent storage is required and there are a low number of users.
  • 需要持久存储和一些小数量的用户
  • The overhead of using a database is significant.
  • 在上面使用数据库是有意义的
  • No database facility exists.
  • 没有数据库设备存在

For more information about when to use isolated storage, see Scenarios for Isolated Storage on MSDN. When configured to use isolated storage, the backing store is isolated by the cache instance name, the user name, the assembly, and the application domain.

更多关于什么时候使用独立存储的信息,请看MSDNScenarios for Isolated Storage。当配置使用独立存储的时候,通过使用缓存实例名、用户名、元数据和应用程序域,后台存储是独立的,

Note:

The isolated storage backing store is not compliant with Federal Information Processing Standards (FIPS). The .NET Framework IsolatedStore relies on non–FIPS-certified cryptography.

独立后台存储和FIPS是不相适应的。.net框架IsolatedStore没有被FIPS鉴定。

Isolated storage is suitable for smart clients and for server applications where each application domain has its own cache. Also note that because isolated storage is always segregated by user, server applications must impersonate the user who is making a request to the application.

独立存储适合于智能客户端和服务器程序,在那里面每一个程序域都有它自己的缓存。也要注意因为独立存储总是被人为隔离,所以服务器应用程序必须模拟用户制造应用程序的请求。

 Using the Data Access Application Block Backing Store

使用数据访问应用程序块回传数据

By using the Data Access Application Block, you can store cached data in a database. Currently, the Caching Application Block includes a script to create the required database schema for SQL Server, and the application block has been tested against SQL Server databases. Developers can use other database types as backing stores, but they must modify the application block source code. Each database type must have a database provider for the Data Access Application Block and include a compatible schema.

The Data Access Application Block backing store option is suitable for smart clients and for server applications where each application domain has its own cache and where you have access to a database.

通过使用数据访问程序应用块,你能在数据库中存储高速缓存数据。通常情况,高速缓存应用块包含一个脚本为数据库建立需要的数据库计划。应用块已被测试过在没有SQL Server数据库的情况下。开发者能够使用其它的数据库类型作为后台存储,但是他们必须修改应用块的源代码。每一个数据库类型必须为数据访问块提供一个数据库提供者,包含一个一致的计划。数据访问应用块后台存储选项适合于智能客户端和服务器应用程序,每个应用域有它自己的缓存,你也可以访问数据库。

Each CacheManager object that is running in a single application domain must use a different portion of the database. A portion is defined as a combination of the application name and the cache instance name. The database can be running on the same server as the application using the cache or on a different server. The number of applications using a cache that the database can support depends only on the database's storage limits.

每一个运行着单应用域的CacheManager对象必须使用数据库的不同部分。一部分被定义为应用程序名称和缓存实例名称的结合。数据库能运行在同一个相同的服务器作为使用缓存的程序或不同的服务器中。应用程序使用数据库能够支持的缓存的数量仅仅取决于数据库的存储容量。

 Considerations for Server Scenarios

服务器场景的考虑

A single cache manager cannot be shared across application domains. Server applications that are deployed on multiple computers have a unique copy of an in-memory cache on each computer. This is also true for multiple processes running on the same computer, including Enterprise Services components that each run in their own process and use the Caching Application Block. Each process has its own copy of the in-memory cache.

一个单独的缓存管理器横跨应用域是不能被共享的。服务器应用程序被配置在多计算机中,在每个计算机有一个内存高速缓存的唯一拷贝。在同一个计算机的多进程也是这样,包括每一个运行在他们自己进程中的使用高速缓存应用块的企业服务组件,每一个进行有它自己的在内存缓存中的拷贝。

Different applications should not use the same Data Access Application Block backing store instance and partition. Running different applications with the Caching Application Block configured to use the same database instance and partition can cause unpredictable results and is not recommended.

不同的程序不应当使用相同的数据访问存储块后台存储实例和部分。运行不同的具备高速缓存应用块的程序配置成使用相同的数据库实例和部分,能够引起不确定的结果,也不推荐这样使用。

When the same application runs in multiple processes (for example, if the application is deployed on multiple computers in a Web farm), you can configure the Caching Application Block in one of three ways:

当相同的程序运行在多进程的时候(例如,如果程序在一个web farm中被配置到多计算机中),你能配置高速缓存应用块使用下面三种方法中的其中一种:

  • All instances of the application use the same database instance, but each instance of the application uses a different database partition. For more information, see Scenario One: Partitioned Caches.
  • 应用程序的所有实例使用相同的数据库实例,但是每一个应用程序的实例使用不同的数据库部分。更多信息,请参见Scenario One: Partitioned Caches
  • All instances of the application use the same database instance and the same database partition and all cache managers can read from and write to the cache. For more information, see Scenario Two: Shared Partition.
  • 所有的程序实例使用相同的数据库实例、相同的数据库部分,所有的缓存管理器能够读和写缓存。更多信息,请参见Scenario Two: Shared Partition.
  • All instances of the application use the same database instance and the same database partition and only one cache manager can write to the cache. All cache managers can read from the cache. For more information, see Scenario Three: Single Writer.
  • 所有程序实例使用相同的数据库实例和相同的数据库部分。但只有一个高速缓存管理器能够写高速缓存。所有的高速缓存管理器能够读缓存。更多信息,参见Scenario Three: Single Writer

Scenario One: Partitioned Caches

场景一:分割缓存

Scenario One is the case where all instances of the application use the same database instance but each instance of the application uses a different database partition. In this scenario, each cache manager operates independently. Although they share the same backing store database instance, each cache manager persists the cache data to a different partition. In effect, there is one cache for each application instance. When an application restarts, each cache manager loads its data from its own partition in the backing store.

场景一是这样一个案例,所有的程序实例使用相同的数据库实例,但是每个程序实例使用不同的数据库部分。在这个场景中,每一个高速缓存管理器操作是独立的。虽然他们共享相同的后台存储数据库的实例,每一个缓存管理器保持高速缓存数据到一个不同的部分。这是有效的,为每个程序实例保持一个缓存。当应用程序重启的时候,每一个高速缓存应用管理器从后台存储加载自己的那一部分。

If the application preloads the cache, each deployed instance of the application retrieves the data from the original data source. The preloaded data uses backing store storage space for each deployed application instance. This means that in terms of using the cache, deploying the same application to multiple processes is no more efficient than deploying different applications.

如果程序重新加载缓存,每一个被配置的程序实例从原始数据源接收数据,重新加载的数据为每一个配置的应用程序实例使用后台存储空间。这意味着使用缓存期间,配置多进程的同一应用程序比配置不同程序的效率更低。

Deploying the same application to multiple servers, where each Configuration Application Block is configured identically (for example, all the application blocks have the same expiration policy), does not guarantee that the data in each backing store partition is identical. The data in the backing store partition duplicates the in-memory cache data of the cache manager configured to use that backing store partition. The contents of the in-memory cache vary according to how a particular instance of the application uses the cache. Because application requests are routed to different servers, the in-memory cache on each server is likely to be different. Therefore, the contents of the backing store partitions are also likely to be different. This means that even if all the applications are shut down and restarted at the same time, there is no guarantee that they will have identical data in their in-memory caches after each cache is initialized with data from the backing store.

配置同一数据库到多服务器中,每一个配置应用程序块是被同样的配置(例如,所有的应用程序块有相同的终止策略),不能保证每个后台存储部分的数据都是相同的。后台存储部分的数据复制缓存管理器配置的内存缓存数据去使用后台存储部分。内存缓存中的内容改变依照一个特殊的程序实例如何使用缓存。因为程序请求被发送到不同的报务器上,在每一个服务器的内存中的缓存可能是不同的。因此,后台存储部分的内容也可能是不同的。这意味着即使所有的程序在同一时间开启和关闭,也不能保证他们将有同样的内存缓存数据在每一个缓存和后台存储的数据初始化之后。

Scenario Two: Shared Partition

场景二:共享分割

Scenario Two is the case where all instances of the application use the same database instance and the same database partition and all cache managers can read from and write to the cache. In this scenario, each instance of an application operates against a unique in-memory cache. When an application creates a cache manager, the cache manager populates the in-memory cache with the data in the backing store. This means that if an application creates a cache manager when it starts, and if all of the application instances are started at the same time, each in-memory cache will be loaded with identical data. Because the applications are using the same partition, each application instance does not require additional storage in the backing store.

场景二是这样一个实例,所有的程序实例使用同一个数据库实例、同一个数据库部分,所有的缓存管理器能够读写缓存。在这个场景中,每一个程序实例的操作不依靠唯一的内存缓存。当程序建立缓存管理器的时候,缓存管理器操作内存缓存中的数据在后台存储中。这意味着当它启动时如果程序建立一个缓存管理器的话,如果所有的程序实例在相同的时间启动,每一个内存缓存都用相同的数据加载。因为程序使用相同的部分,每一个程序实例不需要额外的后台存储。

The only time data is loaded from the backing store into the in-memory cache is when the cache manager is created. After this, the in-memory cache contents are determined by the application instance using the cache. The way an instance of the application uses the cache can vary from one instance to another because requests are routed to different servers. Different instances of an executing application can have in-memory caches with different contents.

当缓存管理器建立的时候,仅仅那个期间数据从后台存储中加载到内存缓存中。在这以后,通过程序实例使用缓存决定内存缓存的内容。应用程序实例使用缓存的方法可能从一个实例到另一个实例是不同的。因为需求被发送到不同的服务器上。一个可招行程序的不同实例能在内存中有不同的内容。

As an application adds and removes items, the contents of the in-memory cache change. The in-memory cache contents also change when the cache manager removes or scavenges expired items. As the in-memory cache changes, the cache manager updates the backing store to reflect these changes. The backing store does not notify cache manager instances when its contents have changed. Therefore, when one application instance changes the backing store contents, the other application instances will have in-memory caches that do not match the backing store data. This means that after an application restarts, the in-memory cache can have contents that are different from the contents it contained before the application restarted.

当一个程序加载和移除条目的时候,内存缓存的内容将发生变化。当缓存管理器移除或清理有效期终止条目时,内存缓存内容也发生变化。由于内存缓存发生变化,高速缓存管理器更新后台存储去反映这些变化。当它的内容发生变化的时候后台存储没有通知高速缓存管理器实例。因此,当应用程序实例改变后台存储内容的时候,其它的有内存缓存的程序实例将和后台存储数据不匹配。这意味着一个程序重启了以后,内存缓存中的内容将和它重启之前的内容不一样。

Applications can be notified when an item expires by subscribing to events that are provided by the cache manager. An application can use this notification to refresh the cache with data from the original data source. When the application adds the refreshed cache item to the cache, the cache manager also updates the backing store with this data. If the application is deployed to multiple computers, each instance of the application can receive the event and initiate requests to the original data source for the same item. These multiple requests can negatively impact the performance of both the application and the original data source. Therefore, using notifications to monitor expirations for the purpose of refreshing expired cache items is not recommended in this scenario.

被缓存管理器提供的当一个条目通过订阅事件终止时,程序能够接收到通知。应用程序能够使用这个通知从原始数据源刷新缓存数据。当程序加载刷新后的缓存条目到缓存中时,缓存管理器也用这个数据更新后台缓存。如果程序被部署到多台计算机中,每一个程序实例能够接收事件,为每一个相同的条目初始化需求到原始数据源。这些多需求消极地影响程序和原始数据源的性能。因此,在这个场景中,使用通知监视刷新终止条目的目的不被推荐。

Scenario Three: Single Writer

场景三:单写入

Scenario Three is the case where all instances of the application use the same database instance and the same database partition and only one cache manager can write to the cache. All cache managers can read from the cache. In this scenario, only one instance of the application writes to the cache. All other application instances can only read from the cache. The instance of the application that writes to the cache is the master. The in-memory cache of the master is always identical to the data in the backing store. The in-memory cache in each application instance is populated with data from the backing store at the time the cache manager is created. The application instances that can only read data from the cache receive a snapshot of the data. Because the application instances do not have the ability to refresh their caches, their caches becomes stale and shrink as items expire.

场景三是这样一个案例,所有程序的实例使用相同的数据库实例、相同的数据库部分,仅仅一个缓存管理器能写入这个缓存。所有的缓存管理器能从缓存中读数据。在这个场景中,仅仅一个程序实例写入缓存。所有的其它程序实例仅仅从缓存中读。写入缓存的程序实例是主要的。在内存的主要缓存总是和后台存储是相同的。在缓存管理器建立的时候,每一个程序实例的内存缓存从后台缓存中装载数据。仅仅从缓存中读取数据的程序实例接收一个数据快照。因为程序实例没有能力刷新它们的缓存,当条目终止时他们的缓存变得过旧。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值