class ActiveSupport::Cache::MongoDbStore

Attributes

collection_name[R]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/active_support/cache/mongo_db_store.rb, line 13
def initialize(options={})
  @collection_name = options[:collection] || :rails_cache
  options[:expires_in] ||= 1.day
  super(options)
end

Public Instance Methods

cleanup(_) click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 23
def cleanup(_)
  collection.find(expires_at: {'$lt' => Time.now.utc.to_i}).delete_many
end
clear() click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 19
def clear
  collection.find.delete_many
end
delete_entry(key, _) click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 34
def delete_entry(key, _)
  collection.find(_id: key).delete_one
end
delete_matched(matcher, options=nil) click to toggle source

@param [Regexp] matcher @param [Hash] options

# File lib/active_support/cache/mongo_db_store.rb, line 29
def delete_matched(matcher, options=nil)
  options = merged_options(options)
  collection.find(_id: key_matcher(matcher, options)).delete_many
end

Protected Instance Methods

read_entry(key, _) click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 54
def read_entry(key, _)
  expiration_criteria = {'$gt' => Time.now.utc.to_i}
  doc = collection.find(_id: key, expires_at: expiration_criteria).first
  rescue_error_with(nil) { deserialize_entry(doc['data']) } if doc
end
write_entry(key, entry, options) click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 40
def write_entry(key, entry, options)
  expires_at = entry.expires_at.to_i
  created_at = Time.now.utc.to_i
  value = options[:raw] ? entry.value.to_s : entry
  rescue_error_with false do
    set = {data: Base64.encode64(Marshal.dump(value)),
           expires_at: expires_at,
           created_at: created_at}
    collection.find(_id: key).update_one({'$set' => set},
                                         upsert: true)
  end
  entry
end

Private Instance Methods

collection() click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 62
def collection
  ::Mongoid.default_client[collection_name]
end
deserialize_entry(raw_value) click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 66
def deserialize_entry(raw_value)
  entry = Marshal.load(Base64.decode64(raw_value))
  entry.is_a?(Entry) ? entry : Entry.new(entry)
end
rescue_error_with(fallback) { || ... } click to toggle source
# File lib/active_support/cache/mongo_db_store.rb, line 71
def rescue_error_with(fallback)
  yield
rescue StandardError => e
  Rails.logger.error("Error (#{e}): #{e.message}")
  fallback
end