您的位置:首页 > 编程语言 > ASP

ASP.NET简单实现服务端图片缓存

2010-07-13 17:48 399 查看
前段时间,一个图片服务器的磁盘IO队列过高,原因是访问量比较高,达到2500 req/s。webserver用的是IIS,磁盘是普通的SATA硬盘。为了解决此问题,想到了缓存。缓存解决方案有很多种,例如:SQUID、ASP.NET+memcached、ASP.NET页面缓存等等。简单起见,我采用了ASP.NET页面缓存来实现。

话不多说,先上解释具体的实现方法。

在项目里新增一个imgcache.aspx页面,页面上设置页面缓存,暂时设为600秒。

<%@ OutputCache Duration="600" VaryByParam="path" %>

在上面这行里应该看到path了吧,path即图片的路径。程序会读取这个文件进行缓存起来,并且输出到浏览器。

在imgcache.aspx.cs文件里,首页获取path的值,然后用FileStream读取此文件,再就是输出到Response.OutputStream。

调用例子:http://image.xxxxxxx.com/imgcache.aspx?path=/images/defaultsingerhead.jpg

具体代码如下:

imgcache.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class ImgCache : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = Request.QueryString["path"];
if (string.IsNullOrEmpty(path) || !File.Exists(Server.MapPath(path)))
{
Response.StatusCode = 404;
}
else
{
FileStream myStream = new FileStream(Server.MapPath(path), FileMode.Open, FileAccess.Read, FileShare.Delete);
myStream.Seek(0,SeekOrigin.Begin);
byte[] data = Kugou.Util.Globals.StreamToBytes(myStream, 0, (int)myStream.Length);
myStream.Close();
Response.ContentType = "image/png";
Response.OutputStream.Write(data, 0, data.Length);
}
}
}


imgcache.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImgCache.aspx.cs" Inherits="ImgCache" %>
<%@ OutputCache Duration="600" VaryByParam="path" %>


此方案最大的特点就是简单,快速实现了缓存图片的功能。应用此方案之后,磁盘队列很快就降下来了。

当然,此方案还不是很完美,存在以下不足:

1、不适合做分布式的缓存,要是分布式的用Memcached会好很多;

2、输出的图片在浏览器端不缓存,原因是使用ASP.NET服务端页面缓存后的http有问题,HTTP头如下:

(Status-Line) HTTP/1.1 200 OK

Connection close

Date Tue, 13 Jul 2010 09:37:49 GMT

Server Microsoft-IIS/6.0

X-Powered-By ASP.NET

X-AspNet-Version 2.0.50727

Cache-Control public, max-age=307

Expires Tue, 13 Jul 2010 09:42:56 GMT

Last-Modified Tue, 13 Jul 2010 09:32:56 GMT

Vary *

Content-Type image/png

Content-Length 959

如果把OutputCache的Location属性改成Client,就可以在浏览器端缓存。<%@ OutputCache Duration="600" VaryByParam="path" Location="Client"%>。这样改了之后输出的HTTP头如下:

(Status-Line) HTTP/1.1 200 OK

Connection close

Date Tue, 13 Jul 2010 09:44:15 GMT

Server Microsoft-IIS/6.0

X-Powered-By ASP.NET

X-AspNet-Version 2.0.50727

Cache-Control private, max-age=600

Expires Tue, 13 Jul 2010 09:54:15 GMT

Last-Modified Tue, 13 Jul 2010 09:44:15 GMT

Content-Type image/png

Content-Length 959

这两个HTTP头的差别在于,第一个多了行Vary *。还有Cache-Control的值不一样。

使用了浏览器端缓存之后,服务器端就没缓存。Location属性还有个值为ServerAndClient,设了之后浏览器不缓存。可能是我愚见了。。

用Cache对象进行缓存可以解决这个问题。以后再改了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: