利用缓存来提高网站的性能(Caching to Improve the Performance of Your Website )
2012-04-28 11:05
495 查看
您可以提高您站点的访问速度,那么就需要用到cache(缓存),通常可以我们使用cache来处理经常检索的结果或不经常改变的数据结果。 你将学习: l 如何使用缓存来提高网站的响应速度。 本章介绍的功能: l WebCache helper |
每当人们请求您站点中的页面时,Web服务器都会做一些工作,以便执行请求。对于其中一些包括有服务器端执行代码的页面,Web服务器可能会执行很长时间来完成任务。比如从数据库中检索数据。这些任务获取在较少数量的的请求时并不需要太长的时间,但是如果网上有了大量的流量,所有用户都进行了独立请求的时候,会导致Web服务器增加很多的工作,从而使执行速度变的缓慢。这最终会影响到网站的性能。
在这种情况下,改善您网站性能的方法之一便是缓存数据。如果您的网站上有在请求时响应的是相同数,且数据并不需要针对个人进行改变,另外在时间上也并不敏感。那么我们可以将数据缓存,在用户每次请求时并不在Web服务器上重新计算,而是在第一次访问时就将数据放到缓存中,在下一次请求时,只获取缓存中的数据。
在一般情况下,您缓存的信息并不经常改变。当你把信息放到缓存,它将存储在Web服务器上的内存中。你可以指定它应该被缓存多久,从几秒钟到几天。当缓存期限到期,该信息自动从缓存中删除。
注意 缓存中的项目并没有过期,但可能会因为其他原因可能会被删除。例如,Web服务器可能暂时低位运行内存,它回收内存的方法之一便是移出缓存项。正如你看到的,即使你放入缓存信息,你必须检查,以确保它在您使用时仍然存在。 |
ASP.NET包括WebCache helper,使得您很容易地将缓存功能添加到网站,并添加到缓存中的数据。在此过程中,您将创建一个页面用来缓存的当前的时间。当然这不是一个真实的例子,因为当前的时间是不经常改变,而且不需要复杂的计算。然而,这是一个很好的方式来说明缓存在工作。
1. 添加一个新页面网站名为WebCache.cshtml。
2. 在页面中添加以下代码和标记:
@{
var cacheItemKey = "Time";
var cacheHit = true;
var time = WebCache.Get(cacheItemKey);
if (time == null) {
cacheHit = false;
}
if (cacheHit == false) {
time = @DateTime.Now;
WebCache.Set(cacheItemKey, time, 1, false);
}
}
<!DOCTYPE html>
<html>
<head>
<title>WebCache Helper 例子</title>
</head>
<body>
<div>
@if (cacheHit) {
@:Found the time data in the cache.
} else {
@:Did not find the time data in the cache.
}
</div>
<div>
页面上的时间被组成存为: @time.
</div>
</body>
</html>
在cacheItemKey代码示例中,使用time定义了一个缓存项,当您缓存数据时,需要为缓存定义一个名称,在整个网站中,这个缓存是独立存在的。
该代码首先读取time缓存项中的值,如果返回值不为空,说明代码从缓存项目中获取了time缓存,并保存到变量time中。
然而,如果缓存条目不存在(也就是说,它是空的),代码会设置time的值为当前时间,将它添加到缓存中,并设置一分钟的到期时限。如果页面请求没有在一分钟之内进行,那么缓存中的项目将被丢弃。(默认缓存项到期时间是20分钟)。
此代码说明的问题是您应该总是使用时缓存数据。在您得到的新的缓存之前,总是先检查WebCache.Get方法是否返回null值。请记住,缓存条目可能因为其他一些原因已过期或可能已被删除,所以从来不能保证任何给定的项目在缓存一直存在。
3. 在浏览器中运行WebCache.cshtml。(请确保页面在选定的工作区中,然后再运行它。)当您第一次请求页面时,时间数据不在缓存中,代码将添加时间值到缓存中。
4. 在浏览器中刷新WebCache.cshtml。这一次,时间数据是从缓存中获取的。请注意你最后一次浏览网页以来的时间一直没有改变。
5. 等待一分钟后缓存被清空,然后刷新页面。再次表明时间数据在缓存中没有找到,更新的时间将被添加到缓存项中。
原文:
http://www.asp.net/web-pages/tutorials/performance-and-traffic/15-caching-to-improve-the-performance-of-your-website
资源:
第7章 - 显示图表中的数据
相关文章推荐
- Performing a deep fetch of the `master` specs repo to improve future performance
- How to user SSE2 instructions to improve the performance of memory copy?
- How to Improve the Performance of CLR
- The Complete Book of Intelligence Tests: 500 Exercises to Improve, Upgrade and Enhance Your Mind Strength (The IQ Workout Series
- Using AJAX to Improve the Bandwidth Performance of Web Applications
- Performing a deep fetch of the `master` specs repo to improve future performance
- Ten ways to improve the performance of large tables in MySQL--转载
- [jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- How to improve the performance of MB5B
- Java Tip 26: How to improve Java's I/O performance-如何提高java I/O性能
- Performing a deep fetch of the `master` specs repo to improve future performance
- 【Asp.Net从零开始】:使用缓存来提高网站性能 Caching(一)
- Tips to improve the performance of ASP.Net Application
- 【转】[jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- 【Asp.Net从零开始】:使用缓存来提高网站性能 Caching(二)
- CSV ----- To improve the performance of Excel Output with reflection (Part 1)
- 20个小技巧提高你的asp.net应用程序性能(20 Tips to Improve ASP.net Application Performance)
- Please download the latest version of the lscn to continue your development.
- Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "te
- [站点推荐]001.学习新技能的37个最佳网站(The 37 Best Websites To Learn Something New)