Assuming you’ve followed the Caching Your Photographs tutorial at some point, you’ll probably have had a lot of fun either deleting the cache every time you upload a new photo or you’ve written your own automated method by now. For those of you that haven’t written your own method of dumping the cache yet, here’s how I do it.
First of all, I created a ‘lib/actions’ folder in the root of my rails project. Inside this folder I created the file ‘photography_action.rb’ with the following contents:
The above fragment naming assumes that your photos are on a page called ‘photography’ if they are elsewhere, change the fragment to expire that page instead.
Fairly simple I think you’ll agree, you may also be asking yourself ‘why the extra file?’, the main reason for the new file is so that the cache clearing can be executed from a new rake task that doesn’t remove all your cached pages or from an admin page on the website.
You’ll also need to update your ‘config.load_paths’ in ‘environment.rb’. After updating, mine looks like this:
Inside some action in some, preferably protected, controller somewhere, add the following (redirecting to anywhere you fancy):
Now for the rake task. Inside the directory ‘lib/tasks’ (create it if it doesn’t exists) create the file ‘photography.rake’ then put the following code inside the file:
You should then be able to run:
From the base of your project in order to clear the cache.
Bear in mind, the code above is literally just a convenient way of clearing out the fragment cache so new photos show up on your photo page, it does not delete photos, nor does it perform a refresh automatically, although, you could add it to a CRON job.
When I get chance, I intend to automate this process and build it into rFlickr along with a new, improved, caching mechanism. I’m sure the above will tide you over for now though.
Check back soon.