How To Prevent Caching

Rails has some pretty strong caching mechanisms. As of Rails 2.0.1 (or thereabouts), caching is sometimes too persistent. The asset id's are the numbers following an image url:

/images/thumbnails/0000/0070/image7_thumb.jpg?1221837030

Here, ?1221837030 is the asset id for this particular image. The asset id is based on the timestamp, or modification date, of the image on the file system. This is good because if you upload a new version of the same picture, the browser will notice the change in the image's url – and load the new version.

Except it's no so good any longer. Starting in version 2.0.1 (or thereabouts) Rails, uhm, caches the asset id. Apparently, this is remedied when you restart the application server. So, every time somebody crops a picture, or rotates it, you just restart your application server.

Fortunately, there's a better solution:

ActionView::Base.computed_public_paths.clear

This forces the asset ids (the timestamps as querystring parameters on img url) to be renewed. Now all is well again.

Further Reading


Personal Tools