Class: Moneta::Adapters::Mongo

Inherits:
Object
  • Object
show all
Includes:
Defaults, ExpiresSupport
Defined in:
lib/moneta/adapters/mongo.rb

Overview

MongoDB backend

Supports expiration, documents will be automatically removed starting with mongodb >= 2.2 (see /).

You can store hashes directly using this adapter.

Examples:

Store hashes

db = Moneta::Adapters::MongoOfficial.new
db['key'] = {a: 1, b: 2}

Constant Summary collapse

DEFAULT_PORT =
27017

Instance Attribute Summary collapse

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from ExpiresSupport

included

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #key?, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :collection (String) — default: 'moneta'

    MongoDB collection name

  • :host (String) — default: '127.0.0.1'

    MongoDB server host

  • :user (String)

    Username used to authenticate

  • :password (String)

    Password used to authenticate

  • :port (Integer) — default: MongoDB default port

    MongoDB server port

  • :db (String) — default: 'moneta'

    MongoDB database

  • :expires (Integer)

    Default expiration time

  • :expires_field (String) — default: 'expiresAt'

    Document field to store expiration time

  • :value_field (String) — default: 'value'

    Document field to store value

  • :type_field (String) — default: 'type'

    Document field to store value type

  • :backend (::Mongo::Client)

    Use existing backend instance

  • Other (Object)

    options passed to `Mongo::MongoClient#new`


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/moneta/adapters/mongo.rb', line 39

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  @expires_field = options.delete(:expires_field) || 'expiresAt'
  @value_field = options.delete(:value_field) || 'value'
  @type_field = options.delete(:type_field) || 'type'

  collection = options.delete(:collection) || 'moneta'
  db = options.delete(:db) || 'moneta'
  @backend = options[:backend] ||
    begin
      host = options.delete(:host) || '127.0.0.1'
      port = options.delete(:port) || DEFAULT_PORT
      options[:logger] ||= ::Logger.new(STDERR).tap do |logger|
        logger.level = ::Logger::ERROR
      end
      ::Mongo::Client.new(["#{host}:#{port}"], options)
    end
  @backend.use(db)
  @collection = @backend[collection]
  if @backend.command(buildinfo: 1).documents.first['version'] >= '2.2'
    @collection.indexes.create_one({ @expires_field => 1 }, expire_after: 0)
  else
    warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
  end
end

Instance Attribute Details

#backendObject (readonly)


22
23
24
# File 'lib/moneta/adapters/mongo.rb', line 22

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})

127
128
129
130
# File 'lib/moneta/adapters/mongo.rb', line 127

def clear(options = {})
  @collection.delete_many
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil


133
134
135
136
# File 'lib/moneta/adapters/mongo.rb', line 133

def close
  @backend.close
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it's not set.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set


117
118
119
120
121
122
123
124
# File 'lib/moneta/adapters/mongo.rb', line 117

def create(key, value, options = {})
  key = to_binary(key)
  @collection.insert_one(value_to_doc(key, value, options))
  true
rescue ::Mongo::Error::OperationFailure => ex
  raise unless ex.message =~ /^E11000 / # duplicate key error
  false
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


100
101
102
103
104
105
106
# File 'lib/moneta/adapters/mongo.rb', line 100

def delete(key, options = {})
  key = to_binary(key)
  if doc = @collection.find(_id: key).find_one_and_delete and
      !doc[@expires_field] || doc[@expires_field] >= Time.now
    doc_to_value(doc)
  end
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)

93
94
95
96
97
# File 'lib/moneta/adapters/mongo.rb', line 93

def each_key
  return enum_for(:each_key) unless block_given?
  @collection.find.each { |doc| yield from_binary(doc[:_id]) }
  self
end

#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.

Overloads:

  • #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

    Returns Array containing the values requested, or where keys are missing, the return values from the corresponding block calls.

    Yield Parameters:

    • key (Object)

      Each key that is not found in the store

    Yield Returns:

    • (Object, nil)

      The value to substitute for the missing one

    Returns:

    • (Array<Object, nil>)

      Array containing the values requested, or where keys are missing, the return values from the corresponding block calls


172
173
174
175
176
177
178
179
180
181
182
# File 'lib/moneta/adapters/mongo.rb', line 172

def fetch_values(*keys, **options)
  return values_at(*keys, **options) unless block_given?
  hash = Hash[slice(*keys, **options)]
  keys.map do |key|
    if hash.key?(key)
      hash[key]
    else
      yield key
    end
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store


109
110
111
112
113
114
# File 'lib/moneta/adapters/mongo.rb', line 109

def increment(key, amount = 1, options = {})
  @collection.find_one_and_update({ :$and => [{ _id: to_binary(key) }, not_expired] },
                                  { :$inc => { @value_field => amount } },
                                  return_document: :after,
                                  upsert: true)[@value_field]
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn't exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/moneta/adapters/mongo.rb', line 66

def load(key, options = {})
  view = @collection.find(:$and => [
                            { _id: to_binary(key) },
                            not_expired
                          ])

  doc = view.limit(1).first

  if doc
    update_expiry(options, nil) do |expires|
      view.update_one(:$set => { @expires_field => expires })
    end

    doc_to_value(doc)
  end
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses Defaults#key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/moneta/adapters/mongo.rb', line 154

def merge!(pairs, options = {})
  existing = Hash[slice(*pairs.map { |key, _| key })]
  update_pairs, insert_pairs = pairs.partition { |key, _| existing.key?(key) }

  @collection.insert_many(insert_pairs.map do |key, value|
    value_to_doc(to_binary(key), value, options)
  end)

  update_pairs.each do |key, value|
    value = yield(key, existing[key], value) if block_given?
    binary = to_binary(key)
    @collection.replace_one({ _id: binary }, value_to_doc(binary, value, options))
  end

  self
end

#slice(*keys, **options) ⇒ <(Object, Object)>

Note:

The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn't be.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (<(Object, Object)>)

    A collection of key-value pairs


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/moneta/adapters/mongo.rb', line 139

def slice(*keys, **options)
  view = @collection.find(:$and => [
                            { _id: { :$in => keys.map(&method(:to_binary)) } },
                            not_expired
                          ])
  pairs = view.map { |doc| [from_binary(doc[:_id]), doc_to_value(doc)] }

  update_expiry(options, nil) do |expires|
    view.update_many(:$set => { @expires_field => expires })
  end

  pairs
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


84
85
86
87
88
89
90
# File 'lib/moneta/adapters/mongo.rb', line 84

def store(key, value, options = {})
  key = to_binary(key)
  @collection.replace_one({ _id: key },
                          value_to_doc(key, value, options),
                          upsert: true)
  value
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values


185
186
187
188
# File 'lib/moneta/adapters/mongo.rb', line 185

def values_at(*keys, **options)
  hash = Hash[slice(*keys, **options)]
  keys.map { |key| hash[key] }
end