Sha256: ad2337ba0e406b390bf4ef0f4b38e7c5aa8754053f00a686d6120ea5be447628
Contents?: true
Size: 1.33 KB
Versions: 16
Compression:
Stored size: 1.33 KB
Contents
begin require "mongo" rescue LoadError puts "You need the mongo gem to use the MongoDB moneta store" exit end module Moneta class MongoDB include Defaults 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 def key?(key) !!self[key] end def [](key) res = @cache.find_first('_id' => key) res = nil if res && res['expires'] && Time.now > res['expires'] res && res['data'] end def []=(key, value) store(key, value) end def delete(key) value = self[key] @cache.remove('_id' => key) if value value end 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 def update_key(key, options = {}) val = self[key] self.store(key, val, options) end def clear @cache.clear end end end
Version data entries
16 entries across 16 versions & 7 rubygems