Class: CachedCounts::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/cached_counts/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Cache

Returns a new instance of Cache.



3
4
5
# File 'lib/cached_counts/cache.rb', line 3

def initialize(scope)
  @scope = scope
end

Instance Method Details

#clearObject

Clear out any count caches which have SQL that includes the scopes table



12
13
14
15
16
17
# File 'lib/cached_counts/cache.rb', line 12

def clear
  invalid_keys = all_keys.select { |key| key.include?(@scope.table_name.downcase.singularize) }
  invalid_keys.each { |key| Rails.cache.delete(key) }

  Rails.cache.write(list_key, all_keys - invalid_keys)
end

#count(*args) ⇒ Object



7
8
9
# File 'lib/cached_counts/cache.rb', line 7

def count(*args)
  cached_count || uncached_count(*args)
end