Class: Moneta::MongoDB
- Inherits:
-
Object
- Object
- Moneta::MongoDB
- Includes:
- Defaults
- Defined in:
- lib/moneta/mongodb.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(options = {}) ⇒ MongoDB
constructor
A new instance of MongoDB.
- #key?(key) ⇒ Boolean
- #store(key, value, options = {}) ⇒ Object
- #update_key(key, options = {}) ⇒ Object
Methods included from Defaults
Constructor Details
#initialize(options = {}) ⇒ MongoDB
Returns a new instance of MongoDB.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/moneta/mongodb.rb', line 12 def initialize( = {}) = { :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', :port => ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT, :db => 'cache', :collection => 'cache' }.update() conn = XGen::Mongo::Driver::Mongo.new([:host], [:port]) @cache = conn.db([:db]).collection([:collection]) end |
Instance Method Details
#[](key) ⇒ Object
27 28 29 30 31 |
# File 'lib/moneta/mongodb.rb', line 27 def [](key) res = @cache.find_first('_id' => key) res = nil if res && res['expires'] && Time.now > res['expires'] res && res['data'] end |
#[]=(key, value) ⇒ Object
33 34 35 |
# File 'lib/moneta/mongodb.rb', line 33 def []=(key, value) store(key, value) end |
#clear ⇒ Object
53 54 55 |
# File 'lib/moneta/mongodb.rb', line 53 def clear @cache.clear end |
#delete(key) ⇒ Object
37 38 39 40 41 |
# File 'lib/moneta/mongodb.rb', line 37 def delete(key) value = self[key] @cache.remove('_id' => key) if value value end |
#key?(key) ⇒ Boolean
23 24 25 |
# File 'lib/moneta/mongodb.rb', line 23 def key?(key) !!self[key] end |
#store(key, value, options = {}) ⇒ Object
43 44 45 46 |
# File 'lib/moneta/mongodb.rb', line 43 def store(key, value, = {}) exp = [:expires_in] ? (Time.now + [:expires_in]) : nil @cache.repsert({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => exp }) end |
#update_key(key, options = {}) ⇒ Object
48 49 50 51 |
# File 'lib/moneta/mongodb.rb', line 48 def update_key(key, = {}) val = self[key] self.store(key, val, ) end |