class Moneta::MongoDB
Public Class Methods
new(options = {})
click to toggle source
# File lib/moneta/mongodb.rb, line 12 def initialize(options = {}) options = { :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', :port => ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT, :db => 'cache', :collection => 'cache' }.update(options) conn = XGen::Mongo::Driver::Mongo.new(options[:host], options[:port]) @cache = conn.db(options[:db]).collection(options[:collection]) end
Public Instance Methods
[](key)
click to toggle source
# 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)
click to toggle source
# File lib/moneta/mongodb.rb, line 33 def []=(key, value) store(key, value) end
clear()
click to toggle source
# File lib/moneta/mongodb.rb, line 53 def clear @cache.clear end
delete(key)
click to toggle source
# File lib/moneta/mongodb.rb, line 37 def delete(key) value = self[key] @cache.remove('_id' => key) if value value end
key?(key)
click to toggle source
# File lib/moneta/mongodb.rb, line 23 def key?(key) !!self[key] end
store(key, value, options = {})
click to toggle source
# File lib/moneta/mongodb.rb, line 43 def store(key, value, options = {}) exp = options[:expires_in] ? (Time.now + options[:expires_in]) : nil @cache.repsert({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => exp }) end
update_key(key, options = {})
click to toggle source
# File lib/moneta/mongodb.rb, line 48 def update_key(key, options = {}) val = self[key] self.store(key, val, options) end