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 docs.mongodb.org/manual/tutorial/expire-data/).
Instance Attribute Summary
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#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
#[], #[]=, #close, #decrement, #fetch, #key?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Mongo
Returns a new instance of Mongo.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/moneta/adapters/mongo.rb', line 21 def initialize( = {}) self.default_expires = .delete(:expires) collection = .delete(:collection) || 'moneta' host = .delete(:host) || '127.0.0.1' port = .delete(:port) || ::Mongo::MongoClient::DEFAULT_PORT db = .delete(:db) || 'moneta' connection = ::Mongo::MongoClient.new(host, port, ) @collection = connection.db(db).collection(collection) if connection.server_version >= '2.2' @collection.ensure_index([['expiresAt', ::Mongo::ASCENDING]], :expireAfterSeconds => 0) else warn '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
78 79 80 81 |
# File 'lib/moneta/adapters/mongo.rb', line 78 def clear( = {}) @collection.remove self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
63 64 65 66 67 |
# File 'lib/moneta/adapters/mongo.rb', line 63 def delete(key, = {}) value = load(key, ) @collection.remove('_id' => ::BSON::Binary.new(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.
70 71 72 73 74 75 |
# File 'lib/moneta/adapters/mongo.rb', line 70 def increment(key, amount = 1, = {}) @collection.find_and_modify(:query => { '_id' => ::BSON::Binary.new(key) }, :update => { '$inc' => { 'value' => amount } }, :new => true, :upsert => true)['value'] end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/moneta/adapters/mongo.rb', line 37 def load(key, = {}) key = ::BSON::Binary.new(key) doc = @collection.find_one('_id' => key) if doc && (!doc['expiresAt'] || doc['expiresAt'] >= Time.now) expires = expires_at(, nil) @collection.update({ '_id' => key }, # expiresAt must be a Time object (BSON date datatype) { '$set' => { 'expiresAt' => expires || nil } }) if expires != nil doc['value'].to_s end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moneta/adapters/mongo.rb', line 50 def store(key, value, = {}) key = ::BSON::Binary.new(key) intvalue = value.to_i @collection.update({ '_id' => key }, { '_id' => key, 'value' => intvalue.to_s == value ? intvalue : ::BSON::Binary.new(value), # expiresAt must be a Time object (BSON date datatype) 'expiresAt' => expires_at() || nil }, { :upsert => true }) value end |