class ActiveSupport::Cache::DatabaseStore
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.
Public Class Methods
new(options = nil)
click to toggle source
param [Hash] options options option options [Class] :model model class. Default: ActiveSupport::Cache::DatabaseStore::Model
Calls superclass method
# File lib/active_support/cache/database_store.rb, line 23 def initialize(options = nil) @model = (options || {}).delete(:model) || Model super(options) end
supports_cache_versioning?()
click to toggle source
Advertise cache versioning support.
# File lib/active_support/cache/database_store.rb, line 17 def self.supports_cache_versioning? true end
Public Instance Methods
cleanup(options = nil)
click to toggle source
Preemptively iterates through all stored keys and removes the ones which have expired.
# 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)
click to toggle source
Clears the entire cache. Be careful with this method.
# 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)
click to toggle source
Calculates the number of entries in the cache.
# 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
Private Instance Methods
delete_entry(key, _options = nil)
click to toggle source
# File lib/active_support/cache/database_store.rb, line 79 def delete_entry(key, _options = nil) @model.where(key: key).destroy_all end
from_record(record)
click to toggle source
# File lib/active_support/cache/database_store.rb, line 107 def from_record(record) return unless record entry = Entry.new Marshal.load(record.value), version: record.version entry.expires_at = record.expires_at entry end
normalize_key(name, options = nil)
click to toggle source
Calls superclass method
# File lib/active_support/cache/database_store.rb, line 62 def normalize_key(name, options = nil) key = super.to_s raise ArgumentError, 'Namespaced key exceeds the length limit' if key && key.bytesize > 255 key end
read_entry(key, _options = nil)
click to toggle source
# File lib/active_support/cache/database_store.rb, line 69 def read_entry(key, _options = nil) from_record @model.where(key: key).first end
read_multi_entries(names, options)
click to toggle source
# File lib/active_support/cache/database_store.rb, line 83 def read_multi_entries(names, options) keyed = {} names.each do |name| version = normalize_version(name, options) keyed[normalize_key(name, options)] = { name: name, version: version } end results = {} @model.where(key: keyed.keys).find_each do |rec| name, version = keyed[rec.key].values_at(:name, :version) entry = from_record(rec) next if entry.nil? if entry.expired? delete_entry(rec.key, **options) elsif entry.mismatched?(version) # Skip mismatched versions else results[name] = entry.value end end results end
write_entry(key, entry, _options = nil)
click to toggle source
# File lib/active_support/cache/database_store.rb, line 73 def write_entry(key, entry, _options = nil) record = @model.where(key: key).first_or_initialize expires_at = Time.zone.at(entry.expires_at) if entry.expires_at record.update! value: Marshal.dump(entry.value), version: entry.version.presence, expires_at: expires_at end