Class: Moneta::Adapters::Mongo
- Inherits:
-
Object
- Object
- Moneta::Adapters::Mongo
- 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.
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the 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 = {}) ⇒ Mongo
constructor
A new instance of Mongo.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #decrement, #features, #fetch, included, #key?, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Mongo
Returns a new instance of Mongo.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moneta/adapters/mongo.rb', line 37 def initialize( = {}) self.default_expires = .delete(:expires) collection = .delete(:collection) || 'moneta' db = .delete(:db) || 'moneta' @expires_field = .delete(:expires_field) || 'expiresAt' @value_field = .delete(:value_field) || 'value' @type_field = .delete(:type_field) || 'type' @backend = [:backend] || begin host = .delete(:host) || '127.0.0.1' port = .delete(:port) || ::Mongo::MongoClient::DEFAULT_PORT user = .delete(:user) password = .delete(:password) ::Mongo::MongoClient.new(host, port, ) end db = @backend.db(db) db.authenticate(user, password, true) if user && password @collection = db.collection(collection) if @backend.server_version >= '2.2' @collection.ensure_index([[@expires_field, ::Mongo::ASCENDING]], :expireAfterSeconds => 0) else warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted' end end |
Instance Attribute Details
#backend ⇒ Object (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
110 111 112 113 |
# File 'lib/moneta/adapters/mongo.rb', line 110 def clear( = {}) @collection.remove self end |
#close ⇒ Object
Explicitly close the store
116 117 118 119 |
# File 'lib/moneta/adapters/mongo.rb', line 116 def close @backend.close nil 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.
100 101 102 103 104 105 106 107 |
# File 'lib/moneta/adapters/mongo.rb', line 100 def create(key, value, = {}) key = to_binary(key) @collection.insert(value_to_doc(key, value, )) true rescue ::Mongo::OperationFailure => ex raise if ex.error_code != 11000 # duplicate key error false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
85 86 87 88 89 |
# File 'lib/moneta/adapters/mongo.rb', line 85 def delete(key, = {}) value = load(key, ) @collection.remove('_id' => to_binary(key)) 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.
92 93 94 95 96 97 |
# File 'lib/moneta/adapters/mongo.rb', line 92 def increment(key, amount = 1, = {}) @collection.find_and_modify(:query => { '_id' => to_binary(key) }, :update => { '$inc' => { @value_field => amount } }, :new => true, :upsert => true)[@value_field] end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/moneta/adapters/mongo.rb', line 63 def load(key, = {}) key = to_binary(key) doc = @collection.find_one('_id' => key) if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now) expires = expires_at(, nil) @collection.update({ '_id' => key }, # @expires_field must be a Time object (BSON date datatype) { '$set' => { @expires_field => expires || nil } }) if expires != nil doc_to_value(doc) end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
76 77 78 79 80 81 82 |
# File 'lib/moneta/adapters/mongo.rb', line 76 def store(key, value, = {}) key = to_binary(key) @collection.update({ '_id' => key }, value_to_doc(key, value, ), { :upsert => true }) value end |