桦山涧

桦山涧
Asp.net ---->知识改变命运!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于Caching的文章两篇,作者翻译的不错!【转】

Posted on 2007-04-04 16:39  桦林  阅读(858)  评论(0编辑  收藏  举报

第一篇文章

最近一段时间看了些缓存的技术文章,顺便翻译一些段落,发现很多东西用中文是如此的难以表达。

Caching

Caching is one of the best techniques you can use to improve performance. Use

caching to optimize reference data lookups, avoid network round trips, and avoid

unnecessary and duplicate processing. To implement caching you need to decide

when to load the cache data. Try to load the cache asynchronously or by using a

batch process to avoid client delays.

缓存技术

缓存是提高应用程序性能的最好方法之一。运用缓存可以优化数据查询,避免不必要的网络数据回传,和避免执行不必要的完全相同的数据处理逻辑。在实现缓存的时候我们要确定什么时候装入缓存数据。用异步装入缓存或用批处理方式来避免出现客户端数据延迟。

When you design for caching, consider the following recommendations:

当我们设计缓存时,要考虑下面几点:

Decide where to cache data. 确定缓存数据结构

Decide what data to cache. 确定缓存什么数据

Decide the expiration policy and scavenging mechanism. 确定缓存过期规则和清理

Decide how to load the cache data. 确定如何装入缓存数据

Avoid distributed coherent caches.

 

Decide Where to Cache Data 确定缓存数据结构

Cache state where it can save the most processing and network round trips. This

might be at the client, a proxy server, your application’s presentation logic, business

logic, or in a database. Choose the cache location that supports the lifetime you want

for your cached items. If you need to cache data for lengthy periods of time, you

should use a SQL Server database. For shorter cache durations, use in-memory

caches.

缓存可以保存的处理结果和网络状态。这些可能是客户端、代理服务、应用程序通用逻辑、业务逻辑或数据库。选择缓存在本地支持————假如要使缓存具有更长的生命周期,可以使用SQL Server数据库,而短的周期则可以缓存在内存中。

Consider the following scenarios: 考虑以下几项

Data caching in the presentation layer. Consider caching data in the presentation

layer when it needs to be displayed to the user and the data is not cached on per-

user basis. For example, if you need to display a list of states, you can look these

up once from the database and then store them in the cache.

For more information about ASP.NET caching techniques, see Chapter 6,

“Improving ASP.NET Performance.”

表现层缓存。当数据要显示绐用户和不是缓存单一用户数据时考虑在表现层缓存。例如,假如我们要显示一个状态列表,我们可以只查询一次数据库然后把它们缓存

Data caching in the business layer. You can implement caching mechanisms

by using hash tables or other data structures in your application’s business logic.

For example, you could cache taxation rules that enable tax calculation. Consider

caching in the business layer when the data cannot be efficiently retrieved from

the database. Data that changes frequently should not be cached.

业务层缓存。可以在应用程序业务逻辑用哈希表或其它数据结构来实现缓存机制。例如,你可以缓存征税规则来计算税率。不能高效的检索数据库数据时应考虑业务层缓存。对变更频繁的数据不能缓存

Data caching in the database. Cache large amounts of data in a database and

when you need to cache for lengthy periods of time. The data can be served in

smaller chunks or as a whole, depending on your requirements. The data will be

cached in temporary tables, which consume more RAM and may cause memory

bottlenecks. You should always measure to see whether caching in a database is

hurting or improving your application performance.

数据库缓存。当数据很大、数据要持久化时应将数据缓存在数据库中。根据需求可以保存数据的一部分或全部。当内存占用率很大或导致内存瓶颈时数据可以缓存到一个临时表中。总是要衡量数据缓存到数据库中会降低还是增加应用程序的性能。

 

Decide What Data to Cache 确定缓存什么数据

Caching the right data is the most critical aspect of caching. If you fail to get this

right, you can end up reducing performance instead of improving it. You might end

up consuming more memory and at the same time suffer from cache misses, where

the data is not actually getting served from cache but is refetched from the original

source.

缓存正确的数据是缓存机制的一个临界点。假如没有设计正确,将降低应用程序的性能而不是提升性能。你将消耗更多的内存和在大部分时间内不命中缓存,数据事实上已经不是读取缓存而是要从数据源中重新读取。



第二篇文章

大量的网站页面是采用动态的方式,根据用户提交的不同请求创建生成页面。正如我们所知的,动态页面有助于根据用户要求来提供定制的动态内容。动态页面也利于获取在数据库中每时每刻更新的资料。缺点是为每个用户请求生成同一页面增加了系统开销。

为克服此问题,一些网站用页面生成引擎对所有页面生成html静态页面。但这样生成的页面对所有用户的请求内容都是相同。

ASP.NET 提供了缓存技术有助于我们最大程度地解决这个问题。它能缓存输出的页面,保存在存储器当中,缓存用户请求的内容。缓存的特点可以根据不方式来定制的。

缓存一个页面
为缓存一个页面输出的内容,我们要在面面顶部指定一个 @OutputCache 命令。语法如下所示:

<%@ OutputCache Duration=5 VaryByParam="None" %>

如你所见,这条指令有两个属性。他们是:

Duration - 被缓存输出的内容有效期是多少秒。在时间超过指定的有效期后,过期的缓存内容会删除,并会在下次用户请求中重新调用页面生成缓存内容。再次的缓存内容过10秒后此过程又会重复。
VaryByParam - 这个属性是必须的,并且指明查询字串参数不同使缓存产生变化。在上面的代码片断中,我们指定 VaryByParam 属性是 “None” ,这意味着不论传递的查询字串参数如何不同所返回的页面内容是相同的。
如果对同一页面的两次请求含不同的查询字串参数,如:.../PageCachingByParam.aspx?id=12 和.../PageCachingByParam.aspx?id=15 ,这应该生成不同的页面内容,指令应该是:

<%@ OutputCache Duration=10 VaryByParam="id" %>

对这样两种不同请求的页面内容每一个都会被缓存 Duration 属性指定的时间长度。。

为了指定多个参数的情形,要使用分号来分隔参数名称。如果我们指定 VaryByParam 属性为“*”,缓存内容会因传递的不同查询字串参数而不同。

一些动态页面要根据不同的浏览来生成不同的内容。在这种情况下就要规定缓存输出内容要因不同浏览器而不同。@OutputCache 命令要改为:

<%@ OutputCache Duration=5 VaryByParam="id" VaryByCustom="browser" %>

这条指令不仅让缓存输出内容因浏览器不同而不同,而且也会因浏览器版本不同而不同,如 IE5、IE 6、Netscape 4、Netscape 6 都会获得不同输出的缓存版本。


缓存局部页面
有时我们可能只是想缓存一个页面的一小部分。例如,我们可能用这样的页面,对所有浏览用户都有同样的内容,这个页面有一个标题。这个标题可能是某个文本/图象组成,并且每天数据都可能改变。在这种情况下,我们就会只想缓存这个标题一天的有效期。

解决方法是把标题内容放入一个用户控件当中,然后指定这个用户控件应该被缓存。这个技术被称为局部缓存(fragment caching)。

为了指定应该被缓存的用户控件,我们利用 @OutputCache 指令,就象整个页面缓存的用法一样。

<%@ OutputCache Duration=10 VaryByParam="None" %>

在上面的指令中,用户控件缓存有效期是 Duration 属性指定的时间(10 秒)。不论查询字串和浏览器类型/版本如何,缓存输出的内容是相同的。。


数据缓存
ASP.NET 也支持作为对象类型数据的缓存。我们可以把对象存储在内存中,在我们应用程序的不同动态页面中使用它们。利用 Cache 类可以实现这个特点。缓存的生存周期与应用程序的相同。对象可以以键值对(name value pairs)的形式储存于缓存当中。如下所示插入一个字串进缓存:

Cache["name"]="Smitha";

这个存储的字串值可以象这样得到:

if (Cache["name"] != null)
Label1.Text= Cache["name"].ToString();

为把对象插入缓存中,可以使用 Cache 类的 Add 方法或者不同版本的 Insert 方法。这些方法可能允许我们使用这个 Cache 类提供的更强大功能,下面是利用了 Insert 方法的一个重载:

Cache.Insert("Name", strName,
new CacheDependency(Server.MapPath("name.txt"),
DateTime.Now.AddMinutes(2), TimeSpan.Zero);

头两个参数是是键名和被插入的对象。第三个参数是 CacheDependency 类型,帮我们对名为 name.txt 文件设置值的依赖项。因此一旦这个文件发生改变,在缓存中的这个值会被删除。我们可以指定 “null” 值来表明没有依赖项。第四个参数指定了从缓存删除这个值的时间。最后一个参数是缓存过期时间可调的参数,它指明了从最后一次访问缓存时间到它被删除的时间间隔。

当可用系统内存不多时,缓存自动从内存中删除使用少的项目。这个过程被称为净化(scavenging)。我们可以为添加进缓存中的项目指定优先级,以便某些项目能得到优先处理:

Cache.Insert("Name", strName,
new CacheDependency(Server.MapPath("name.txt"),
DateTime.Now.AddMinutes(2), TimeSpan.Zero,
CacheItemPriority.High, null);

枚举类型 CacheItemPriority 给成员设置不同的优先级。CacheItemPriority.High 给项目分配了个高优先级,以使该项目减少被缓存删除的可能性。


要点
如果你还是老的 ASP 页面并且利用 Response.Expires 属性缓存页面输出,那么他们也可以象 ASP.NET 支持这个属性一样被保留。
Cache 类的 Insert 方法会用相同键名的项目覆盖任何已存在项目。
CacheItemPriority.NotRemovable 优先值可以与 Cache.Insert 方法一起用于设置一个项目的优先级,以便这个项目不会在净化期间从缓存中被删除

结语
本文中,我尽可能对 ASP.NET 的缓存技术多个可选方便提供一个总的概括。为保持本文的简短扼要没有提供详细的解释和说明。

在一个启用缓存的含子控件嵌套风格中可用局部缓存。我还没有测试如何利用这一点,因此也就没有包括这个技巧的说明。同时 Cache 类的 Insert 方法也就没有在此讨论了。我希望本文对读者而言是一个研究多姿多彩的 ASP.NET 世界的一个良好的开端。