Class: ActiveSupport::Cache::DatabaseStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::DatabaseStore
- 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
-
.supports_cache_versioning? ⇒ Boolean
Advertise cache versioning support.
Instance Method Summary collapse
-
#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.
-
#clear(options = nil) ⇒ Boolean
Clears the entire cache.
-
#count(options = nil) ⇒ Object
Calculates the number of entries in the cache.
-
#decrement(name, amount = 1, options = nil) ⇒ Object
Decrements an integer value in the cache.
-
#increment(name, amount = 1, options = nil) ⇒ Object
Increments an integer value in the cache.
-
#initialize(options = nil) ⇒ DatabaseStore
constructor
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.
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
26 27 28 29 30 31 32 33 |
# File 'lib/active_support/cache/database_store.rb', line 26 def initialize( = nil) @model = ( || {}).delete(:model) || Model @compression = ( || {}).delete(:compression)&.to_s raise ArgumentError, "invalid compression option #{@compression.inspect}" if @compression && !COMPRESSION_HANDLERS.key?(@compression) super() end |
Class Method Details
.supports_cache_versioning? ⇒ Boolean
Advertise cache versioning support.
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( = nil) = () scope = @model.expired if (created_before = [:created_before]) scope = scope.or(@model.created_before(created_before)) end if (namespace = [: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
61 62 63 64 65 66 67 68 69 |
# File 'lib/active_support/cache/database_store.rb', line 61 def clear( = nil) = () if (namespace = [: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( = nil) = () scope = @model.all if (namespace = [:namespace]) scope = scope.namespaced(namespace) end scope = scope.fresh unless [: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, = nil) increment(name, -amount, ) 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, = nil) = () scope = @model.all if (namespace = [:namespace]) scope = scope.namespaced(namespace) end entry = Entry.new(amount, **.merge(version: normalize_version(name, ))) attrs = { key: normalize_key(name, ), **entry_attributes(entry) } scope.upsert(attrs, on_duplicate: Arel.sql(sanitize_sql_array(['value = EXCLUDED.value + ?', amount]))) end |