Class: Moneta::Adapters::Mongo
- Inherits:
-
Object
- Object
- Moneta::Adapters::Mongo
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/mongo.rb
Overview
MongoDB backend
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.
-
#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, #increment, #key?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Mongo
Returns a new instance of Mongo.
15 16 17 18 19 20 21 22 |
# File 'lib/moneta/adapters/mongo.rb', line 15 def initialize( = {}) collection = .delete(:collection) || 'moneta' host = .delete(:host) || '127.0.0.1' port = .delete(:port) || ::Mongo::Connection::DEFAULT_PORT db = .delete(:db) || 'moneta' connection = ::Mongo::Connection.new(host, port, ) @collection = connection.db(db).collection(collection) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
47 48 49 50 |
# File 'lib/moneta/adapters/mongo.rb', line 47 def clear( = {}) @collection.remove self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
31 32 33 34 35 |
# File 'lib/moneta/adapters/mongo.rb', line 31 def delete(key, = {}) value = load(key, ) @collection.remove('_id' => ::BSON::Binary.new(key)) if value value end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
25 26 27 28 |
# File 'lib/moneta/adapters/mongo.rb', line 25 def load(key, = {}) value = @collection.find_one('_id' => ::BSON::Binary.new(key)) value && value['value'].to_s end |
#store(key, value, options = {}) ⇒ Object
Store value with key
38 39 40 41 42 43 44 |
# File 'lib/moneta/adapters/mongo.rb', line 38 def store(key, value, = {}) key = ::BSON::Binary.new(key) @collection.update({ '_id' => key }, { '_id' => key, 'value' => ::BSON::Binary.new(value) }, { :upsert => true }) value end |