Class: Moneta::Adapters::Mongo
- Defined in:
- lib/moneta/adapters/mongo.rb
Overview
MongoDB backend
Instance Method Summary collapse
- #clear(options = {}) ⇒ Object
- #delete(key, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Mongo
constructor
Constructor.
- #load(key, options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
Methods inherited from Base
#[], #[]=, #close, #decrement, #fetch, #increment, #key?
Methods included from Mixins::WithOptions
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Mongo
Constructor
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 = {}) ⇒ Object
43 44 45 46 |
# File 'lib/moneta/adapters/mongo.rb', line 43 def clear( = {}) @collection.remove self end |
#delete(key, options = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/moneta/adapters/mongo.rb', line 29 def delete(key, = {}) value = load(key, ) @collection.remove('_id' => ::BSON::Binary.new(key)) if value value end |
#load(key, options = {}) ⇒ Object
24 25 26 27 |
# File 'lib/moneta/adapters/mongo.rb', line 24 def load(key, = {}) value = @collection.find_one('_id' => ::BSON::Binary.new(key)) value && value['value'].to_s end |
#store(key, value, options = {}) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/moneta/adapters/mongo.rb', line 35 def store(key, value, = {}) key = ::BSON::Binary.new(key) @collection.update({ '_id' => key }, { '_id' => key, 'value' => ::BSON::Binary.new(value) }, { :upsert => true }) value end |