Class: Moneta::Adapters::MongoMoped
- Defined in:
- lib/moneta/adapters/mongo/moped.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.
Constant Summary
Constants inherited from MongoBase
Moneta::Adapters::MongoBase::DEFAULT_PORT
Instance Attribute Summary
Attributes inherited from MongoBase
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ MongoMoped
constructor
A new instance of MongoMoped.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #decrement, #features, #fetch, included, #key?, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ MongoMoped
Returns a new instance of MongoMoped.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 32 def initialize( = {}) super() collection = .delete(:collection) || 'moneta' db = .delete(:db) || 'moneta' user = .delete(:user) password = .delete(:password) @backend = [:backend] || begin host = .delete(:host) || '127.0.0.1' port = .delete(:port) || 27017 ::Moped::Session.new(["#{host}:#{port}"]) end @backend.use(db) @backend.login(user, password) if user && password @collection = @backend[collection] if @backend.command(buildinfo: 1)['version'] >= '2.2' @collection.indexes.create({ @expires_field => 1 }, expireAfterSeconds: 0) else warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted' end end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
101 102 103 104 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 101 def clear( = {}) @collection.drop self end |
#create(key, value, options = {}) ⇒ Boolean
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.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 89 def create(key, value, = {}) key = to_binary(key) @backend.with(safe: true, consistency: :strong) do |safe| safe[@collection.name].insert(value_to_doc(key, value, )) end true rescue ::Moped::Errors::MongoError => ex raise if ex.details['code'] != 11000 # duplicate key error false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
74 75 76 77 78 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 74 def delete(key, = {}) value = load(key, ) @collection.find(_id: to_binary(key)).remove if value value end |
#increment(key, amount = 1, options = {}) ⇒ Object
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.
81 82 83 84 85 86 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 81 def increment(key, amount = 1, = {}) @backend.with(safe: true, consistency: :strong) do |safe| safe[@collection.name].find(_id: to_binary(key)).modify({:$inc => { @value_field => amount }}, new: true, upsert: true)[@value_field] end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 55 def load(key, = {}) key = to_binary(key) doc = @collection.find(_id: key).one if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now) # @expires_field must be a Time object (BSON date datatype) expires = expires_at(, nil) @collection.find(_id: key).update(:$set => { @expires_field => expires || nil }) if expires != nil doc_to_value(doc) end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
67 68 69 70 71 |
# File 'lib/moneta/adapters/mongo/moped.rb', line 67 def store(key, value, = {}) key = to_binary(key) @collection.find(_id: key).upsert(value_to_doc(key, value, )) value end |