Enterprise Library-Introduction to the Caching Application Block

Introduction to the Caching Application Block

This topic includes a series of brief sections that provide information to help you decide whether the

这个主题包括了一系列的简短章节,提供了一些信息帮助你决定了缓冲应用块是否适合于你的需求。

Caching Application Block is suitable for your requirements. This topic includes the following sections:

这个主题包括了下面几节:

通用场景

应用代码例子

缓存应用块集锦

决定什么时候使用缓存应用块

有选择的使用缓存应用块

安全

In addition to this introductory material, the documentation also contains the following topics:

这篇介绍材料之外,这份文档也包含了下面的主题:

使用缓存应用块开发应用程序。这个主题首先解释怎么样配置缓存应用块和如何加载到你的程序中。还解释了如何选择一个存储备份。

  • Key Scenarios. This section demonstrates how to use the application block to perform typical caching operations.

关键场景。此节示范了如何使用此应用程序块去执行典型的缓存应用。

设计缓存应用块。这个主题解释了怎么样去设计应用程序块和决策背后的原理。

扩展和配置缓存应用块。这个主题解释了如何扩展应用程序块通过加入你自己的后台存储和你自己的终止策略。也解释了如何配置它通过改变源代码的方式。

部署和操作。这个主题阐述了如何部署和更新你的应用程序块部件。

  • Caching QuickStart. This topic explains how to install and configure the QuickStart application and contains a series of walkthroughs that demonstrate how to incorporate common caching operations into an application.

缓存QuickStart.这个主题解释了如何安装和配置QuickStart应用和保持了一系列的预演来示范怎么样合并通用的缓存操作到一个程序中。

For details of the system requirements for the Caching Application Block, see System Requirements. For details of the dependencies for the Caching Application Block, see Application Block Dependencies.

缓存应用块的机器详细配置需求,请看System Requirements。缓存应用块的依赖细节,请看Application Block Dependencies

 Common Scenarios

通用场景

The Caching Application Block is suitable if you encounter any of the following situations:

如果你遇到下面的场景,缓存应用块是适应使用的:

  • You must repeatedly access static data or data that rarely changes.

你不得不反复的存取静态数据或者数据很少发生变化。

  • Data access is expensive in terms of creation, access, or transportation.

数据存取是昂贵的在建立、存取或者传输。

  • Data must always be available, even when the source, such as a server, is not available.

You can use the Caching Application Block with any of the following application types:

数据总是可使用的,甚至当源数据也不可用的时候(例如服务器也不可用)。你可以使用缓存应用块在下面任何一种应用当中。

  • Windows Forms

胖客户端的winform应用。

  • Console application

控制台的应用

  • Windows service

Windows服务

  • COM+ server

COM+服务器

  • ASP.NET Web application or Web service if you need features not included in the ASP.NET cache

Asp.netWEB程序或者是你需要一些具备不包含asp.net的缓存特性的web service时。

You should deploy the Caching Application Block within a single application domain. Each application domain can have one or multiple caches, either with or without backing stores. Caches cannot be shared among different application domains.

你应当在一个单一的应用域中配置缓存应用块。每一个应用域能有一个或多个缓存,也可能有或没有后台存储。缓存不能在不同的应用域中进行共享。

The Caching Application Block is optimized for performance and is both thread safe and exception safe. You can extend it to include your own expiration policies and your own backing store.

缓存应用块也是可以为了性能进行调节的。并且它也是线程安全的、有异常安全处理的。你能扩展它,把它加入到你自己的终止策略和你自己的后台存储。

 Example Application Code

例子应用代码

The following code shows how to add an item to a cache and retrieve an item from the cache. It creates an object of type Product and then adds it to the cache with a scavenging priority of 2, an instruction not to refresh the item if it expires, and an expiration date of 5 minutes from the last time the item was accessed.

下面的代码显示了如何加一个条目到缓存中,和从缓存中接收一个条目。它建立一个product的类别对象。然后把它加到缓存中并赋于优先级别2。如果它终止了,指令不会刷新条目,5分钟的有效期从最后一次条目被访问开始。

Note:

The code does not include the Product class definition.

 

C#

Copy Code

ICacheManager productsCache = CacheFactory.GetCacheManager();

 

string id = "ProductOneId";

string name = "ProductXYName";

int price = 50;

 

Product product = new Product(id, name, price);

 

productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(5)));

 

// Retrieve the item.

product = (Product) productsCache.GetData(id);

Visual Basic

Copy Code

Dim productsCache As ICacheManager = CacheFactory.GetCacheManager()

 

Dim id As String = "ProductOneId"

Dim name As String = "ProductOneName"

Dim price As Integer = 50

 

Dim newProduct As Product = New Product(id, name, price)

 

productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, Nothing, New SlidingTime(TimeSpan.FromMinutes(5)))

 

' Retrieve the item.

product = DirectCast(productsCache.GetData(id), Product)

 Highlights of the Caching Application Block

缓存应用块集锦

The Enterprise Library Caching Application Block includes the following features:

企业库缓存应用块包括下面特性

  • You can use the graphical Enterprise Library configuration tools to manage configuration settings.

你能使用图形企业库配置工具管理配置设置

  • You can configure a persistent storage location, using either isolated storage or the Enterprise Library Data Access Application Block, whose state is synchronized with the in-memory cache.

你能配置一个持久化的存储单元,用于独立存储或企业库的数据访问应用块。他们的状态可以和内存缓存进行同步。

  • You can extend the application block by creating custom expiration policies and storage locations.

你能通过建立自定义的终止策略和存储位置来扩展应用块。

  • You receive assurance that the application block performs in a thread-safe manner.

你可以确保应用块的执行是线程安全的。

 Determining When to Use the Caching Application Block

决定什么时候使用缓存应用块

The Caching Application Block is designed to work in the most common data-caching situation, which is when the application and the cache exist on the same system. This means that the cache is local and should be used only by that application. When it operates within these guidelines, the application block is ideal for addressing the following situations:

缓存应用块是被设计工作在大多数的数据缓存位置,它处于当应用和缓存块存在相同的系统中时。这意味着缓存是本地的,它应当被仅仅被那个程序所使用。当遵循这些原则来使用时,在下面的场景中应用块是理想的选择。

  • The situation requires a consistent form of caching across different application environments. For example, by using the Caching Application Block, developers can write similar code to implement caching in application components hosted in Internet Information Services (IIS), Enterprise Services, and smart client environments. Also, the same cache configuration options exist for all environments.

需要一致的缓存形式可以穿越不同的应用环境。例如,开发者能够写相似的代码去实现程序组件的缓存在IIS、企业服务和智能客户端。同样,相同的缓存配置选项存在所有的环境中。

  • The situation requires a configurable and persistent backing store. The Caching Application Block supports both isolated storage and database backing stores. Developers can create additional backing store providers and add them to the Caching Application Block using its configuration settings. The application block can also symmetrically encrypt a cache item's data before it is persisted to a backing store.

需要一个可配置的和持久层的后台存储。缓存应用块支持独立存储和数据库后台存储。开发者能够建立额外的后台存储提供者,把它们加到高速缓存应用块中通过使用它的配置设置。应用块也能够同步加密一个缓存项的数据在它被持久化到后台存储之前。

  • The situation requires that changing the cache configuration settings will not require application source code changes. Developers first write the code that uses one or more named caches. System operators and developers can then configure each of these named caches differently using the Enterprise Library configuration tools.

改变高速缓存的设置不将需要进行源代码的改动。开发者首先写代码使用一个或多个缓存来进行命名。系统操作者和开发者可以配置不同命名的缓存通过使用企业库的配置工具。

  • Cache items require any of the following expiration settings: absolute time, sliding time, extended time format (for example, every evening at midnight), file dependency, or never expired. For more information about using the expiration settings, see Design of the Expiration Process.

缓存项需要下面任何一种终止设置绝对时间、变化时间、扩展时间格式(例如,每一个晚上的半夜),文件依赖,或从不终止。更多的关于使用终止设置的信息,请参见Design of the Expiration Process.

In addition, the Caching Application Block provides a development model consistent with the other Enterprise Library Application Blocks. The Caching Application Block integrates seamlessly with the Data Access Application Block for backing store functionality. In the same manner, the Security Application Block includes caching capability that is provided by the Caching Application Block. Developers and operators configure the application block using the Enterprise Library configuration tools.

另外,高速缓存应用块提供了一个和其它企业库应用块一致的开发模型。高速缓存应用块和数据访问应用块无缝结合形成了后台存储的功能。同样地,安全应用块包含了缓存能力,它通过使用高速缓存应用块被提供。开发者和操作者通过使用企业库配置工具来对应用块进行配置。

 Alternatives to Using the Caching Application Block

选择性的使用高速缓存应用块

Situations that would be better served by other caching solutions are when there are multiple applications using the cache or when the cache and the application are not on the same system. For example, you cannot synchronize caching across a Web farm. However, you can replace the CacheManager class with a custom class if you need to fundamentally change the behavior of the application block. For more information, see Extending the Caching Application Block.

通过使用其它的缓存解决方案能够更好的工作。当并行程序使用缓存或缓存和程序不在同一个系统时。例如,你不能跨越web form进行同步缓存。然而,如果你需要根本性的改变应用块的行为你能用一个自定义类替换那个cachemanager类。更多信息,请看Extending the Caching Application Block

ASP.NET Cache

The .NET Framework includes the ASP.NET cache in the System.Web namespace. ASP.NET application developers access this cache through the System.Web.HTTPContext.Cache object. The ASP.NET cache was developed and tuned for ASP.NET applications. However, this cache can also be used outside of an ASP.NET application by accessing the System.Web.HTTPRuntime.Cache object. The ASP.NET cache requires the System.Web assembly. Developers should verify that the assembly is supported on the required platforms and in the target environments.

.net framework包括了asp.net缓存在system.web命名空间。Asp.net程序开发者访问这个缓存通过使用system.web.httpcontext.cache对象。Asp.net缓存被开发以适用用在asp.net应用程序中。然而,通过使用system.web.httpruntime.cache对象这个缓存也能被用于在非asp.net程序。Asp.net缓存需要system.web集合。开发者应当检查那个集合在需要的平台和目标环境中是否被支持。

 Security

安全性

Although you can encrypt data cached in the backing stores, the Caching Application Block does not

虽然你可以在后台存储中加密数据缓存,高速缓存应用块不支持加密的数据在内存中被缓存。

support encryption of data that is cached in memory. If a malicious user finds a way of compromising the

如果恶意用户发现了系统的漏洞并且访问你的程序进程中的内存,他或她将可能访问你在高速缓存中存储的信息。

system and accessing the memory of your application's process, he or she would be able to access information stored in the cache. If this is a significant threat to your application, do not store sensitive

如果对于你的程序这是一个严重的威胁,不要存储敏感信息,例如信用卡号码或密码在你的缓存中。

 information, such as credit card numbers or passwords, in the cache.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值