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

Class Method Summary collapse

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



23
24
25
26
# File 'lib/active_support/cache/database_store.rb', line 23

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

Class Method Details

.supports_cache_versioning?Boolean

Advertise cache versioning support.

Returns:



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

def self.supports_cache_versioning?
  true
end

Instance Method Details

#cleanup(options = nil) ⇒ Object

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



29
30
31
32
33
34
35
36
# File 'lib/active_support/cache/database_store.rb', line 29

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.



39
40
41
42
43
44
45
46
47
# File 'lib/active_support/cache/database_store.rb', line 39

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.



50
51
52
53
54
55
56
57
58
# File 'lib/active_support/cache/database_store.rb', line 50

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