Module: KaminariCache

Extended by:
ActiveSupport::Concern
Defined in:
lib/kaminari-cache.rb,
lib/kaminari-cache/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/kaminari-cache.rb', line 4

def self.included(base)
  base.after_update :flush_pagination_cache
end

Instance Method Details

#flush_pagination_cacheObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kaminari-cache.rb', line 8

def flush_pagination_cache
  store_class = Rails.cache.class.name.split("::").last
  if Rails.cache.respond_to? "delete_matched"

    self.changed.each do |field|
      match = [:kaminari, "/#{self.class.name.downcase}", '*', "/#{:order}", '*', "/#{field}", '*']
      
      Rails.logger.info "Cache flush: #{match.join}"
      case store_class
      when "DalliStore"
        # DalliStore uses a Regexp
        regexp = Regexp.new match.join.gsub(/\*/,'.*?')
        Rails.cache.delete_matched /^#{Regexp.escape(:kaminari)}\/#{Regexp.escape(self.class.name.downcase)}/
      when "RedisStore"
        # RedisStore uses a String
        Rails.cache.delete_matched match.join
      end
    end

  else
    Rails.cache.clear
    Rails.logger.warn "#{store_class} does not support deleting matched keys. The entire cache was flushed. Consider using RedisStore (Redis) OR DalliStore (gem dalli) for memcached with dalli-store-extensions gem for improved performance."
  end
end