class ActiveSupport::Cache::MongoDBCacheStore

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/active_support/cache/mongo_db_cache_store.rb, line 9
def initialize(options = {})
  options[:db_uri] ||= 'mongodb://127.0.0.1:27017/rails_cache_store'
  options[:collection_name] ||= :rails_caches
  options[:expires_in] ||= 86400
  super(options)
  @client = Mongo::Client.new(@options[:db_uri])
end

Public Instance Methods

cleanup() click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 21
def cleanup
  collection.find(expires_at: {'$lt' => Time.now.utc.to_f}).delete_many
end
clear() click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 17
def clear
  collection.delete_many
end
collection() click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 60
def collection
  @client[@options[:collection_name]]
end
delete_entry(key, options = nil) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 43
def delete_entry(key, options = nil)
  collection.find(_id: key).delete_one
end
delete_matched(matcher, options=nil) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 47
def delete_matched(matcher, options=nil)
  options = merged_options(options)
  collection.find(_id: key_matcher(matcher, options)).delete_many
end
deserialize(value) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 52
def deserialize(value)
  Marshal.load(value) rescue value
end
read_entry(key, options) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 25
def read_entry(key, options)
  query = {_id: key, expires_at: {'$gt': Time.now.to_f}}
  doc = collection.find(query).first
  return nil if doc.nil?
  entry = doc['raw'] ? doc['value'] : deserialize(doc['value'].data)
  entry.is_a?(Entry) ? entry : Entry.new(entry)
end
serialize(value) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 56
def serialize(value)
  BSON::Binary.new(Marshal.dump(value))
end
write_entry(key, entry, raw: false, **options) click to toggle source
# File lib/active_support/cache/mongo_db_cache_store.rb, line 33
def write_entry(key, entry, raw: false, **options)
  query   = {_id: key}
  updates = {'$set': {
      value:  raw ? entry.value : serialize(entry),
      expires_at: entry.expires_at,
      raw: raw
  }}
  collection.update_one(query, updates, upsert: true)
end