Asp.net 2.0 的缓存技术

Performance is the key requirement of any application. One of important technique which helps in the performance of application is Caching. Caching is the process of storing frequently used data on the server to fulfill subsequent requests. Reusing pages from memory is much faster than re-creating pages every time they are requesting. Caching increases your application's performance, scalability and availability. In ASP.NET, caching is implemented as an HttpModule that listens to all HttpRequests that come through the ASP.NET worker process.

性能是任何程序的最关键需求, 缓存是提高程序性能的一项重要的技术。 缓存是存储那些经常用的数据在服务器上满足那些并发需求。从内存中重用页面,比那些为新的请求重新创建新的页面速度快多了。 缓存提高了你程序的性能,伸缩性和实用行。 在ASP.NET 中。缓存作为HttpModule 监听所有的HttpRequest的方式实现的。
我们将讨论多种缓存技术包括了 新的SQL缓存过期 和回发缓存置换。我们现在稍微关注普通的缓存依赖。

 

缓存的方式

我们有几种处理缓存的不同方式,下面我们给出几种方法

  • 输出缓存(Output Caching)

  • 部分页面缓存(Partial Page Caching)

  • 数据缓存(Data Caching)

输出缓存

Output Caching is a way to keep the dynamically generated page content in the server's memory to fulfill future requests. You apply output caching by inserting an OutputCache page directive at the top of an .aspx page as shown below.

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

The Duration attribute defines the number of seconds a page is stored in memory.

Attributes of Output Caching

The VaryByParam attribute determines which versions of the page output are actually cached. You can generate different responses based on whether an HTTP-POST or HTTP-GET response is required. You can also cache different responses based on different attributes like VaryByHeader, VaryByCustom, VaryByControl.

The VaryByParam attribute can specify which QueryString parameters cause a new version of the page to be cached.

<%@ OutputCache Duration="60" VaryByParam = "ProductID;CategoryID"  %>

For example, if you have a page called products.aspx that includes products and category information in the QueryString such as productID and CategoryID. The page directive, shown above caches the products page for every different value of CategoryID and ProductID. This way the number of pages cached can be calculated using below simple equation

Cached Items = (number of categories) * (number of products)

Cached Items is the number of pages that would be stored in cache.

If you want to cache a new version of the page based on any combination of parameters, you can use VaryByParam ="*", as shown below

<%@ OutputCache Duration="60" VaryByParam = "*"  %>

VaryByControl

VaryByControl can be easy way to get good performance from complex user controls that render a lot of HTML that doesn't change often. For example, imagine a User Control that renders a Combo Box showing the names of all the countries in the world. Certainly, the names of countries don't change that often. So you can set cache based on this user Control where duration attribute can be set to say one fortnight.

<%@ OutputCache Duration="1296000" VaryByControl="drpdownCountries"  %>

VaryByCustom

Although the VaryBy Attribute offer a great deal of flexibility, but you need more flexibility. If you want to take the OutputCache directive from products page and cache a value stored in a cookie, you can add VaryByCustom. The value of VaryByCustom is passed into the GetVaryByCustomString method that can be added to the Global.asax file. This method is called every time the page is requested, and it is the function's responsibility to return a value. A different version of the page is cached for each unique values returned. For example, say your users have a cookie called colors that has three potential values: blue, yellow and green. You want to allow users to specify their preferred color. Therefore, the OutputCache directive using the first example caches many versions of the page as given below in the equation:

Cached Items = (num of categoryID's) * (num of productID's) * ( 3 possible colors)

To sum up, suppose there were ten potential values for CategoryID, four potential ProductID for each CategoryID and three possible color values. This adds up to 120 different potential cached versions of this single product page.

Caching is tradeoff between CPU and memory. Every page request served from the cache saves a round trip to database. Efficient use of caching can translate into significant cost savings.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值