Clearing Out The rFlickr Cache

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;
class PhotographyAction
def self.clear_cache
ActionController::Base.new.expire_fragment(%r{photography.cache})
end
endThe 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;
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers #{RAILS_ROOT}/lib/actions )Inside some action in some, preferably protected, controller somewhere, add the following (redirecting to anywhere you fancy);
PhotographyAction.clear_cache
redirect_to :action => 'index'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;
namespace :photo do
namespace :cache do
desc "Clear out photography cache"
task :clear => :environment do
PhotographyAction.clear_cache
end
end
endYou should then be able to run;
rake photo:cache:clear
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.


