Skip to content

Instantly share code, notes, and snippets.

@KensoDev
Created March 19, 2012 21:50
Show Gist options
  • Save KensoDev/2127397 to your computer and use it in GitHub Desktop.
Save KensoDev/2127397 to your computer and use it in GitHub Desktop.
Cache control
module CacheControl
extends self
def expire_cache_for(model, options = {})
Rails.logger.debug "+++ CACHECONTROL - Expiring cache for #{model.class} #{model.id rescue nil} with options #{options.inspect}"
model_name = model.class.name
begin
invalidator_class = "CacheControl::#{model_name}".contantize
invalidator_class.invalidate_cache(model, option)
rescue Exception => e
Rails.logger.debug "+++ CACHECONTROL - Don't know how to expire cache for #{model.class} #{model.id rescue nil} with options #{options.inspect}"
Airbrake.notify(
error_class: "CacheControl",
error_message: "Don't know how to expire cache for #{model.class}",
params: {model: model, options: options}
)
end
end
def expire_user_homepage_feed(user_id)
Rails.logger.debug "Invalidate user_profile_activity_logs_#{user_id}"
# since this sweeper is included withing every controller thru the ApplicationController,
# expire_fragment doesn't seem to work. so using regular Rails.cache.delete with 'views/' prefix
# expire_fragment("user_profile_activity_logs_#{activity_log.user_id}")
Rails.cache.delete("views/user_profile_activity_logs_#{user_id}") # specific user data in profile
Rails.cache.delete("views/user_home_activity_logs_#{user_id}") # full home feed for a user
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment