Caching uploaded images... and resetting cache them when new image is uploaded. RRS feed

  • Question

  • User1407395795 posted
    I've got an application where users can upload image files.  The files are normally not in a standard web graphic format, but I need thumbnails and web viewable files.  So I created an HttpHandler that creates the images on the fly.  However this slows the app down a bit, I found it possible to speed this up by playing with teh HttpContext.Response.Cache attribute,  but I can't find a way to reset this cache when the user uploads a new file or removes the old one.  The old thumbnails are visible until the cache expires.  Is there an easy way to force the cache to expire from the upload/remove image page?

    I've looked at the HttpContext.cache object  should I being adding something here from the HttpHandler instead of using the HttpContext.Response.Cache? and removing it in the upload/remove image pages codebehind file?  If so... what should I be adding?  the response created in the HttpHandler? (or the image that was created?) 

    Or is there an easy way to set a dependancy in the HTTPhandler so that if the file the image was created from changes then invalidate that item in the cache?

    Tuesday, April 4, 2006 12:04 PM

All replies

  • User-429982059 posted
    You can use SQL Invalidation in .net 2.0 to cancel your cache out when a change [ new uploaded image] occurs.  You should just store the paths to the images in the db [ or the images in the db] and cache those paths on the user [ as they are looking at them].  Maybe you could cache their most seen photos or the ones they just saw.

    Refer : http://eramseur.blogspot.com/2005/11/sql-server-2005-cache-features.html
    Tuesday, April 4, 2006 2:59 PM
  • User1407395795 posted
    The paths won't ever change though.  The application isn't a typical photo-album application.  The images all have set names/paths, but occasionally the users will need to modify the actual file and upload a new image (but the path *has* to be the same, including the filename).  So what I need is some way to invalidate the cache after the upload.

    I've used:

      Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

    and this works well rather well for speed purposes,  but I can't find a way to invalidate the cache when new images are uploaded.  context.response.cache doesn't seem to be an actual "cache" object, but a cachepolicy.   

    I've tried  using a the Context.cache object (which is an actual cache object).   and storing the image there using a key (the urlpath) and giving it a cachedependancy on the file.  Then the next time I get that request  I would take the image out of the cache instead of generating it.  
    That sort of worked, but wasn't much quicker than not doing it at all (still noticed a pause on the images reloading on every postback) and worse  the first time I loaded a page all the images would load up fine,  after doing a postback some of the images wouldn't show up.

    The only thing I can think to do is change it to:
    So that every post back on the page doesn't require the image to be reconverted/drawn, but the users can still upload/remove files and still see a change reasonably quickly.

    Though its still not what I'd like.

    Tuesday, April 4, 2006 4:35 PM
  • User1407395795 posted
    Ok, I fixed my own problem.  Using the HttpContext.cache object,  I just remove the object from the cache whenever I upload/remove the file.  When I tried to create a dependency using the file  it didn't seem to work so well.
    Wednesday, April 5, 2006 9:19 AM
  • User-429982059 posted
    I say cache the paths [ since they never change] at the Application level and then store the actual file names in the db.  This way you can use SQL Invalidation and the cache will automatically invalidate without any code needed .  This is a new thing with SQL 2005 and asp.net 2.0.  You just point to the table and asp.net can poll or do on event to invalidate the cache.  Cool thing it only takes one user to upload and ALL users get the cache change.
    Thursday, April 6, 2006 6:13 PM
  • User657123739 posted

    This is rather old, but for the sake of reference ....:

    Don't complicate things. Forget sql cache invalidation, and add a version parameter to your image/thumb url, like: ...&ver=0706051023. Create the version number from the ChangedDate field in your database (or last modified date field). If the image used on the page/image browser changes, the output cache is invalid. So, you can set the output cache on images to forever.




    Monday, November 5, 2007 9:41 AM
  • User946268604 posted

    Tad, Could you please help me with the situation. I am stuck in same situation, I would be thankful, if you describe how you can able remove object from cache. (Note: I am trying to use following article to do Image caching http://www.codeproject.com/KB/aspnet/CachingImagesInASPNET.aspx)

     Thanks Every help is much appreciated.

    Tuesday, May 5, 2009 11:52 AM