Search code examples
c#asp.net-mvcimageresizer

Correct way to cache images in MVC app


I am using the ImageResizing library, to resize and deliver my image in my C# Mvc application.

One thing that isn't happening though is my image aren't getting cached.

I am struggling to understand what would be required to appropriately add caching for each image.

I just need to know if I am on the write track? Will this cache my images correctly?

I think what I need to do, is set the FinalContentType, and FinalContentType in my ImageResizer_OnPostAuthorizeRequestStart (I dont know where to get these values)

And then, I am hoping that in the Application_PreSendRequestHeaders I can use the code below to set the cache headers correctly.

I have used a modified version of the method described here.

Here is my code:

 private static void ImageResizer_OnPostAuthorizeRequestStart(IHttpModule sender2, HttpContext context)
        {
            string path = Config.Current.Pipeline.PreRewritePath;
            if (!path.StartsWith(PathUtils.ResolveAppRelative("~/s3"), StringComparison.OrdinalIgnoreCase)) return;
        Config.Current.Pipeline.SkipFileTypeCheck = true;
        Config.Current.Pipeline.ModifiedQueryString["cache"] = ServerCacheMode.Always.ToString();
    }

    protected void Application_PreSendRequestHeaders(Object source, EventArgs e)
    {
        var app = source as HttpApplication;

        HttpContext context = (app != null) ? app.Context : null;

        if (context != null && context.Items != null && context.Items["FinalContentType"] != null && context.Items["LastModifiedDate"] != null)
        {
            //Clear previous output 
            //context.Response.Clear(); 
            context.Response.ContentType = context.Items["FinalContentType"].ToString();

            //FinalContentType is set to image/jpeg or whatever the image mime-type is earlier in code. 
            //Add caching headers 
            int mins = 1; //Or Configuration.AppSettings['whatever'] 

            if (mins > 0)
            {
                context.Response.Expires = 1;
            }

            var lastModified = (DateTime?)context.Items["LastModifiedDate"]; //Set earlier in code. 

            if (lastModified != DateTime.MinValue)
            {
                Response.Cache.SetLastModified(lastModified.Value);
            }

            Response.Cache.SetCacheability(context.Request.IsAuthenticated ? HttpCacheability.Private : HttpCacheability.Public);
        }

    }

Solution

  • Use the DiskCache and ClientCache plugins to handle disk caching and cache headers respectively.

    ASP.NET output caching is useless here.