Caching the book cover images

In my “mybooklist” application I deal with lots of images! My current method has been to store the uploaded book covers in public/images/bookcovers/isbnnumber.png” and creating thumbnails on the fly in the list and grid views!

Of course this is a very bad thing to do! The load on my server was going crazy with all the CPU intensive image manipulation going on! I had to do something.

The task required to different solutions. The first was client side caching to prevent the browser downloading the image multiple times! to achieve this I modified the show browser function like so:

# 2. we see if we need to send the file or not
        lastmod = File.stat(filepath).mtime
        minTime = Time.rfc2822(@request.env["HTTP_IF_MODIFIED_SINCE"]) rescue nil
        if minTime and lastmod < = minTime
            # use cached version
            render_text '', '304 Not Modified'
            # send image

now I only send the image to the client only in I need to!

The second area of improvement was the thumbnail creation on the fly! I first tried the rails

caches_page :show_cover

but I found myself in a situation where I was sending the image to the client every time because rails was sending the cached image without ever running the show_cover function (and my browser cache code)!

I guess I am just going to generate the thumbnails in the create and update functions. The app should feel a lot different and the load on the server should stay low!

Leave a Reply