Reputation: 3882
I'm serving a users default picture from the url /:username/picture (much like facebook). The action takes several get parameters to specify the image dimension and style wanted. Here is an example:
/john/picture?d[]=50&d[]=50&s=square
This would return johns default picture at a size of 50px x 50px cropped as a square.
Hitting the whole rails stack for every picture request is obviously not efficient. I'd like to cache the image versions. This seems possible with something like the solution to this so question.
caches_action :my_action, :cache_path => Proc.new { |c| c.params }
However, I need to find a way to clear the cache for all the image versions when a user changes their default picture. Basically when john changes his default picture I wish I could clear with a regex similar to the below so that all of the default thumbnails will be regenerated with the new default picture:
clear cache /john/picture*
How would I accomplish this in rails?
Upvotes: 4
Views: 932
Reputation: 1
Checkout https://github.com/pennymac/action_param_caching for a simpler version of this.
Upvotes: 0
Reputation: 5060
alongside your cache_index
, you can assign a class to cache_sweeper
in your controller.
# assign the cache_sweeper
caches_action :my_action, :cache_path => Proc.new { |c| c.params }
cache_sweeper :picture_sweeper
then, alongside of your controllers, add in a new class
# class to help with sweeping up cached pictures
class PictureSweeper < ActionController::Caching::Sweeper
observe Picture # or whatever your class is called
# ... be sure to read on how to setup a cache sweeper
def after_update( record )
# you should probably check if record.is_a? Picture or whatever your class is
self.class::sweep( record )
end
# note there is also after_create, after_destroy, after_save
def self.sweep( record )
# the cache directory is in ActionController::Base.page_cache_directory
# use the record to help reconstruct the path to your cached pictures
# and delete whatever you need to
end
# note, i showed the sweeping split out from the after_update method in case you
# wanted to sweep after several of the events... trying to be DRY...
end
Upvotes: 0
Reputation: 1062
I could be misunderstanding your question, but I think you need a sweeper. http://guides.rubyonrails.org/caching_with_rails.html#sweepers
Upvotes: 1