ASP. NET simple server-side image Cache

Source: Internet
Author: User

Some time ago, the disk I/O queue of an image server was too high because of a high access volume, reaching 2500 req/s. The Webserver uses IIS and the disk uses a general SATA hard disk. To solve this problem, I thought of caching. There are many cache solutions, such as squid, ASP. NET + memcached, and ASP. NET page cache. For simplicity, I use ASP. NET page caching.

 

I will explain the specific implementation methods first.

 

Add an imgcache. ASPX page in the project and set the page cache to 600 seconds.

<% @ Outputcache duration = "600" varybyparam = "path" %>

 

In the above line, we should see the path, which is the path of the image. The program will read the file and cache it and output it to the browser.

 

In the imgcache. aspx. CS file, the home page obtains the PATH value, reads the file with filestream, and then outputs it to response. outputstream.

 

Call example: http://image.xxxxxxx.com/imgcache.aspx? Path =/images/defaultsingerhead.jpg

 

The Code is as follows:

 

Imgcache. aspx. CS

 

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. io; <br/> Public partial class imgcache: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> string Path = request. querystring ["path"]; <br/> If (string. isnullorempty (PATH) |! File. exists (server. mappath (PATH) <br/>{< br/> response. statuscode = 404; <br/>}< br/> else <br/>{< br/> filestream mystream = new filestream (server. mappath (PATH), filemode. open, fileaccess. read, fileshare. delete); <br/> mystream. seek (0, seekorigin. begin); <br/> byte [] DATA = kugou. util. globals. streamtobytes (mystream, 0, (INT) mystream. length); <br/> mystream. close (); <br/> response. contenttype = "image/PNG"; <br/> response. outputstream. write (data, 0, Data. length); <br/>}< br/>

 

Imgcache. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "imgcache. aspx. CS "inherits =" imgcache "%> <br/> <% @ outputcache duration =" 600 "varybyparam =" path "%>

 

The biggest feature of this solution is its simplicity and quick implementation of the image caching function. After this scheme is applied, the disk queue will soon be downgraded.

 

Of course, this solution is not perfect and has the following shortcomings:

1. It is not suitable for Distributed caching. It will be much better if memcached is used in a distributed manner;

2. The output image is not cached on the browser because there is a problem with HTTP after the pages are cached on the ASP. NET Server. The HTTP header is as follows:

(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

 

If you change the location attribute of outputcache to client, you can cache it on the browser side. <% @ Outputcache duration = "600" varybyparam = "path" location = "client" %>. The output HTTP header is as follows:

(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

 

The difference between the two HTTP headers is that the first line is added with vary *. The value of cache-control is different.

 

After the browser is used for caching, the server is not cached. The location property also has a value of serverandclient, which is not cached by the browser after it is set. Maybe it's my ignorance ..

 

Cache objects can solve this problem. I will change it later.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.