Class: ActiveSupport::Cache::DatabaseStore

Inherits:
Store
  • Object
show all
Includes:
Strategy::LocalCache
Defined in:
lib/active_support/cache/database_store.rb,
lib/active_support/cache/database_store/model.rb,
lib/active_support/cache/database_store/migration.rb

Overview

A cache store implementation which stores everything in the database, using ActiveRecord as the backend.

DatabaseStore implements the Strategy::LocalCache strategy which implements an in-memory cache inside of a block.

Defined Under Namespace

Classes: Migration, Model

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ DatabaseStore

param [Hash] options options option options [Class] :model model class. Default: ActiveSupport::Cache::DatabaseStore::Model



18
19
20
21
# File 'lib/active_support/cache/database_store.rb', line 18

def initialize(options=nil)
  @model = (options || {}).delete(:model) || Model
  super(options)
end

Instance Method Details

#cleanup(options = nil) ⇒ Object

Preemptively iterates through all stored keys and removes the ones which have expired.



24
25
26
27
28
29
30
31
# File 'lib/active_support/cache/database_store.rb', line 24

def cleanup(options=nil)
  options = merged_options(options)
  scope = @model.expired
  if (namespace = options[:namespace])
    scope = scope.namespaced(namespace)
  end
  scope.delete_all
end

#clear(options = nil) ⇒ Object

Clears the entire cache. Be careful with this method.



34
35
36
37
38
39
40
41
42
# File 'lib/active_support/cache/database_store.rb', line 34

def clear(options=nil)
  options = merged_options(options)
  if (namespace = options[:namespace])
    @model.namespaced(namespace).delete_all
  else
    @model.truncate!
  end
  true
end

#count(options = nil) ⇒ Object

Calculates the number of entries in the cache.



45
46
47
48
49
50
51
52
53
# File 'lib/active_support/cache/database_store.rb', line 45

def count(options=nil)
  options = merged_options(options)
  scope = @model.all
  if (namespace = options[:namespace])
    scope = scope.namespaced(namespace)
  end
  scope = scope.fresh unless options[:all]
  scope.count
end