Class: Moneta::Adapters::MongoOfficial

### Home Classes Methods

### In Files

- lib/moneta/adapters/mongo/official.rb

### Parent

Moneta::Adapters::MongoBase

### Methods

- ::new
- #clear
- #close
- #create
- #delete
- #increment
- #load
- #store
- #to_binary

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Moneta
- Moneta::Adapters
- Moneta::Adapters::ActiveRecord
- Moneta::Adapters::Cassandra
- Moneta::Adapters::Client
- Moneta::Adapters::Cookie
- Moneta::Adapters::Couch
- Moneta::Adapters::DBM
- Moneta::Adapters::DataMapper
- Moneta::Adapters::DataMapper::Store
- Moneta::Adapters::Daybreak
- Moneta::Adapters::File
- Moneta::Adapters::Fog
- Moneta::Adapters::GDBM
- Moneta::Adapters::HBase
- Moneta::Adapters::KyotoCabinet
- Moneta::Adapters::LMDB
- Moneta::Adapters::LRUHash
- Moneta::Adapters::LRUHash::Entry
- Moneta::Adapters::LevelDB
- Moneta::Adapters::LocalMemCache
- Moneta::Adapters::MemcachedDalli
- Moneta::Adapters::MemcachedNative
- Moneta::Adapters::Memory
- Moneta::Adapters::MongoBase
- Moneta::Adapters::MongoMoped
- Moneta::Adapters::MongoOfficial
- Moneta::Adapters::Null
- Moneta::Adapters::PStore
- Moneta::Adapters::Redis
- Moneta::Adapters::RestClient
- Moneta::Adapters::Riak
- Moneta::Adapters::SDBM
- Moneta::Adapters::Sequel
- Moneta::Adapters::Sqlite
- Moneta::Adapters::TDB
- Moneta::Adapters::TokyoCabinet
- Moneta::Adapters::TokyoTyrant
- Moneta::Adapters::YAML
- Moneta::Builder
- Moneta::Cache
- Moneta::Cache::DSL
- Moneta::CreateSupport
- Moneta::Defaults
- Moneta::Defaults::ClassMethods
- Moneta::Expires
- Moneta::ExpiresSupport
- Moneta::HashAdapter
- Moneta::IncrementSupport
- Moneta::Lock
- Moneta::Logger
- Moneta::Logger::Format
- Moneta::Mutex
- Moneta::OptionMerger
- Moneta::OptionSupport
- Moneta::Pool
- Moneta::Proxy
- Moneta::Semaphore
- Moneta::Server
- Moneta::Shared
- Moneta::Stack
- Moneta::Stack::DSL
- Moneta::SynchronizePrimitive
- Moneta::Transformer
- Moneta::Transformer::Helper
- Moneta::Utils
- Moneta::WeakCreate
- Moneta::WeakIncrement
- Moneta::Wrapper
- Rack
- Rack::Cache
- Rack::Cache::EntityStore
- Rack::Cache::EntityStore::MONETA
- Rack::Cache::EntityStore::Moneta
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::MONETA
- Rack::Cache::MetaStore::Moneta
- Rack::Cache::MonetaResolver
- Rack::MonetaCookies
- Rack::MonetaRest
- Rack::MonetaStore
- Rack::Session
- Rack::Session::Moneta
- ActionDispatch
- ActionDispatch::Session
- ActionDispatch::Session::MonetaStore
- ActiveSupport
- ActiveSupport::Cache
- ActiveSupport::Cache::MonetaStore

No matching classes.

# Moneta::Adapters::MongoOfficial

MongoDB backend

Supports expiration, documents will be automatically removed starting with mongodb >= 2.2 (see {docs.mongodb.org/manual/tutorial/expire-data/}).

You can store hashes directly using this adapter.

@example Store hashes

    db = Moneta::Adapters::MongoOfficial.new
    db['key'] = {a: 1, b: 2}

@api public

### Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :collection ('moneta') MongoDB collection name @option options [String] :host ('127.0.0.1') MongoDB server host @option options [String] :user Username used to authenticate @option options [String] :password Password used to authenticate @option options [Integer] :port (MongoDB default port) MongoDB server port @option options [String] :db ('moneta') MongoDB database @option options [Integer] :expires Default expiration time @option options [String] :expires_field ('expiresAt') Document field to store expiration time @option options [String] :value_field ('value') Document field to store value @option options [String] :type_field ('type') Document field to store value type @option options [::Mongo::MongoClient] :backend Use existing backend instance @option options Other options passed to `Mongo::MongoClient#new`

    # File lib/moneta/adapters/mongo/official.rb, line 32
    def initialize(options = {})
      super(options)
      collection = options.delete(:collection) || 'moneta'
      db = options.delete(:db) || 'moneta'
      user = options.delete(:user)
      password = options.delete(:password)
      @backend = options[:backend] ||
        begin
          host = options.delete(:host) || '127.0.0.1'
          port = options.delete(:port) || ::Mongo::MongoClient::DEFAULT_PORT
          ::Mongo::MongoClient.new(host, port, options)
        end
      db = @backend.db(db)
      db.authenticate(user, password, true) if user && password
      @collection = db.collection(collection)
      if @backend.server_version >= '2.2'
        @collection.ensure_index([[@expires_field, ::Mongo::ASCENDING]], expireAfterSeconds: 0)
      else
        warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
      end
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/mongo/official.rb, line 102
    def clear(options = {})
      @collection.remove
      self
    end

close() click to toggle source

(see Proxy#close)

    # File lib/moneta/adapters/mongo/official.rb, line 108
    def close
      @backend.close
      nil
    end

create(key, value, options = {}) click to toggle source

(see Proxy#create)

    # File lib/moneta/adapters/mongo/official.rb, line 92
    def create(key, value, options = {})
      key = to_binary(key)
      @collection.insert(value_to_doc(key, value, options))
      true
    rescue ::Mongo::OperationFailure => ex
      raise if ex.error_code != 11000 # duplicate key error
      false
    end

delete(key, options = {}) click to toggle source

(see Proxy#delete)

    # File lib/moneta/adapters/mongo/official.rb, line 77
    def delete(key, options = {})
      value = load(key, options)
      @collection.remove('_id' => to_binary(key)) if value
      value
    end

increment(key, amount = 1, options = {}) click to toggle source

(see Proxy#increment)

    # File lib/moneta/adapters/mongo/official.rb, line 84
    def increment(key, amount = 1, options = {})
      @collection.find_and_modify(query: { '_id' => to_binary(key) },
                                  update: { '$inc' => { @value_field => amount } },
                                  new: true,
                                  upsert: true)[@value_field]
    end

load(key, options = {}) click to toggle source

(see Proxy#load)

    # File lib/moneta/adapters/mongo/official.rb, line 55
    def load(key, options = {})
      key = to_binary(key)
      doc = @collection.find_one('_id' => key)
      if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now)
        expires = expires_at(options, nil)
        @collection.update({ '_id' => key },
                           # @expires_field must be a Time object (BSON date datatype)
                           { '$set' => { @expires_field => expires || nil } }) if expires != nil
        doc_to_value(doc)
      end
    end

store(key, value, options = {}) click to toggle source

(see Proxy#store)

    # File lib/moneta/adapters/mongo/official.rb, line 68
    def store(key, value, options = {})
      key = to_binary(key)
      @collection.update({ '_id' => key },
                         value_to_doc(key, value, options),
                         { upsert: true })
      value
    end

### Protected Instance Methods

to_binary(s) click to toggle source

    # File lib/moneta/adapters/mongo/official.rb, line 115
    def to_binary(s)
      s = s.dup if s.frozen? # HACK: BSON::Binary needs unfrozen string
      ::BSON::Binary.new(s)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.