Module: CatchCache::Flush
- Defined in:
- lib/catch_cache/flush.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/catch_cache/flush.rb', line 4 def included(klass) klass.class_eval do extend ClassMethods define_method(:flush_cache!) do key_callbacks = ClassMethods.key_callbacks key_callbacks.keys.select{|key| key.to_s.split("__").last == self.class.name.underscore }.each do |key| # Get the uniq id defined in the AR model begin uniq_id = instance_exec(&key_callbacks[key]) # Build the redis cache key cache_key = "#{key.to_s.split("__").first}_#{uniq_id}" redis = Redis.new # Flush the key by setting it to nil redis.set(cache_key, nil) rescue NameError => e # Nothing was flushed because of an error" end end end define_method(:flush_all!) do redis = Redis.new registered_keys = ClassMethods.key_callbacks.keys.map{|key| key.to_s.split("__").first.to_sym }.uniq removable_keys = redis.keys.select do |key| registered_keys.include?(key.gsub(/\_[0-9]+/, '').to_sym) end redis.del(removable_keys) if removable_keys.present? end end end |