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

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: Model

Constant Summary collapse

COMPRESSION_HANDLERS =
{ 'gzip' => ActiveSupport::Gzip }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ DatabaseStore

param [Hash] options options option options [Class] :model (default: ActiveSupport::Cache::DatabaseStore::Model) model class option options [String] :compression (default: nil) Use “gzip” value to compress cache

Raises:

  • (ArgumentError)


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

def initialize(options = nil)
  @model = (options || {}).delete(:model) || Model
  @compression = (options || {}).delete(:compression)&.to_s

  raise ArgumentError, "invalid compression option #{@compression.inspect}" if @compression && !COMPRESSION_HANDLERS.key?(@compression)

  super(options)
end

Class Method Details

.supports_cache_versioning?Boolean

Advertise cache versioning support.

Returns:

  • (Boolean)


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

def self.supports_cache_versioning?
  true
end

Instance Method Details

#cleanup(options = nil) ⇒ Object

params [Hash] options option options [String] :namespace option options [ActiveSupport::Duration] :created_before - provide a time duration after record without expire_at date will get removed.



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

def cleanup(options = nil)
  options = merged_options(options)
  scope = @model.expired

  if (created_before = options[:created_before])
    scope = scope.or(@model.created_before(created_before))
  end

  if (namespace = options[:namespace])
    scope = scope.namespaced(namespace)
  end

  scope.delete_all
end

#clear(options = nil) ⇒ Boolean

Clears the entire cache. Be careful with this method.

params [Hash] options option options [String] :namespace

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/active_support/cache/database_store.rb', line 61

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.



72
73
74
75
76
77
78
79
80
# File 'lib/active_support/cache/database_store.rb', line 72

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

#decrement(name, amount = 1, options = nil) ⇒ Object

Decrements an integer value in the cache.



97
98
99
# File 'lib/active_support/cache/database_store.rb', line 97

def decrement(name, amount = 1, options = nil)
  increment(name, -amount, options)
end

#increment(name, amount = 1, options = nil) ⇒ Object

Increments an integer value in the cache.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_support/cache/database_store.rb', line 83

def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  scope = @model.all
  if (namespace = options[:namespace])
    scope = scope.namespaced(namespace)
  end

  entry = Entry.new(amount, **options.merge(version: normalize_version(name, options)))

  attrs = { key: normalize_key(name, options), **entry_attributes(entry) }
  scope.upsert(attrs, on_duplicate: Arel.sql(sanitize_sql_array(['value = EXCLUDED.value + ?', amount])))
end