Sha256: 1c460ad2b8816598c5ea95e79a157c7877dd4ad71c90bcd2828412f4a007c7ff
Contents?: true
Size: 1.03 KB
Versions: 3
Compression:
Stored size: 1.03 KB
Contents
module CachedCounts class Cache def initialize(scope) @scope = scope @args = [] end def count(*args) @args = args cached_count || uncached_count end # Clear out any count caches which have SQL that includes the scopes table def clear invalid_keys = all_keys.select { |key| key.include?(@scope.table_name.downcase) } invalid_keys.each { |key| Rails.cache.delete(key) } Rails.cache.write(list_key, all_keys - invalid_keys) end private def cached_count if all_keys.include?(current_key) Rails.cache.fetch(current_key) end end def uncached_count @scope.count_without_caching(*@args).tap do |count| Rails.cache.write(current_key, count) Rails.cache.write(list_key, all_keys + [current_key]) end end def all_keys Rails.cache.fetch(list_key) || [] end def list_key "cached_counts::keys" end def current_key "cached_counts::#{@scope.to_sql.downcase}::#{@args}" end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
cached_counts-0.3.0 | lib/cached_counts/cache.rb |
cached_counts-0.2.5 | lib/cached_counts/cache.rb |
cached_counts-0.2.4 | lib/cached_counts/cache.rb |