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

Version Path
aeden-moneta-0.6.2 lib/moneta/mongodb.rb
aeden-moneta-0.6.3 lib/moneta/mongodb.rb
benschwarz-moneta-0.6.1 lib/moneta/mongodb.rb
benschwarz-smoke-0.5.0 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.1 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.2 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.3 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.4 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.5 vendor/moneta/lib/moneta/mongodb.rb
benschwarz-smoke-0.5.6 vendor/moneta/lib/moneta/mongodb.rb
jferris-moneta-0.6.2 lib/moneta/mongodb.rb
jsl-moneta-0.6.1 lib/moneta/mongodb.rb
jsl-moneta-0.6.2 lib/moneta/mongodb.rb
jsl-moneta-0.6.3 lib/moneta/mongodb.rb
smurphy-moneta-0.6.0 lib/moneta/mongodb.rb
moneta-0.6.0 lib/moneta/mongodb.rb