Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPX That Returns An Image - Output Cache-able?

Greetings!

I've created an APSX web form that returns a remote image based on some supplied parameters. It can be used like this:

<img src="/ImageGetter.aspx?param1=abc&param2=123" />

ImageGetter.aspx's markup and code look similar to this:

<%@ OutputCache Duration="100000" VaryByParam="*" Location="ServerAndClient" %>
<%@ Page Language="C#" AutoEventWireup="false" EnableSessionState="False" CodeBehind="ImageGetter.aspx.cs" Inherits="ACME.Helpers.ImageGetter" %>

This code is called in ImageGetter.aspx's Page_Load method:

byte[] data = null;
Dictionary<string, string> file_locations = GetImageLocations(param1, param2);
try
{
    data = new WebClient().DownloadData(file_locations["main"]);
}
catch (WebException wex)
{
    try
    {
        data = new WebClient().DownloadData(file_locations["backup"]);
    }
    catch (Exception e)
    {
        throw;
    }
}
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(data, 0, data.Length);
Response.End();

From my testing, it doesn't appear to be caching. Is this possible to do with Output Caching or should I resort to writing my own cache to store the byte arrays depending on query string paramters?

like image 534
Bullines Avatar asked Apr 13 '09 21:04

Bullines


People also ask

What is output cache?

Output caching stores the generated output of pages, controls, and HTTP responses in memory. Output caching enables you to cache different versions of content depending on the query string and on form-post parameters to a page, on browser type, or on the language of the user.

What is output cache in asp net?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

What is IIS output caching?

The IIS output caching feature targets semi-dynamic content. It lets you cache static responses for dynamic requests and increase scalability. Note that not every dynamic page can take advantage of the output cache.

What is data caching in asp net?

Caching enables you to store data in memory for rapid access. When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. This can improve performance and scalability.


2 Answers

Try dropping the Response.End() as this will terminate the thread prematurely and prevent output caching from taking place.

See: http://bytes.com/groups/net-asp/323363-cache-varybyparam-doesnt-work

You may wish to consider using an ASHX handler and using your own caching method.

like image 68
Codebrain Avatar answered Oct 05 '22 08:10

Codebrain


Use an ASHX generic handler and use the HttpRuntimeCache (Cache object) to do the job as Codebrain said. It will be faster and WAY more flexible.

like image 43
Andrei Rînea Avatar answered Oct 05 '22 07:10

Andrei Rînea