Class: Moneta::Adapters::MemcachedNative

### Home Classes Methods

### In Files

- lib/moneta/adapters/memcached/native.rb

### Parent

Object

### Methods

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

### Included Modules

- Moneta::Defaults
- Moneta::ExpiresSupport

### 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::MemcachedNative

Memcached backend (using gem memcached) @api public

### Attributes

backend[R]

### Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :server ('127.0.0.1:11211') Memcached server @option options [String] :namespace Key namespace @option options [Integer] :expires (604800) Default expiration time @option options [::Memcached] :backend Use existing backend instance @option options Other options passed to `Memcached#new`

    # File lib/moneta/adapters/memcached/native.rb, line 20
    def initialize(options = {})
      server = options.delete(:server) || '127.0.0.1:11211'
      self.default_expires = options.delete(:expires)
      @backend = options[:backend] ||
        begin
          options.merge!(prefix_key: options.delete(:namespace)) if options[:namespace]
          # We don't want a limitation on the key charset. Therefore we use the binary protocol.
          # It is also faster.
          options[:binary_protocol] = true unless options.include?(:binary_protocol)
          ::Memcached.new(server, options)
        end
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/memcached/native.rb, line 84
    def clear(options = {})
      @backend.flush
      self
    end

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

(see Defaults#create)

    # File lib/moneta/adapters/memcached/native.rb, line 76
    def create(key, value, options = {})
      @backend.add(key, value, expires_value(options) || 0, false)
      true
    rescue ::Memcached::ConnectionDataExists
      false
    end

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

(see Proxy#delete)

    # File lib/moneta/adapters/memcached/native.rb, line 52
    def delete(key, options = {})
      value = @backend.get(key, false)
      @backend.delete(key)
      value
    rescue ::Memcached::NotFound
    end

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

(see Proxy#increment)

    # File lib/moneta/adapters/memcached/native.rb, line 60
    def increment(key, amount = 1, options = {})
      result = if amount >= 0
        @backend.increment(key, amount)
      else
        @backend.decrement(key, -amount)
      end
      # HACK: Throw error if applied to invalid value
      # see https://github.com/evan/memcached/issues/110
      Utils.to_int((@backend.get(key, false) rescue nil)) if result == 0
      result
    rescue ::Memcached::NotFound => ex
      retry unless create(key, amount.to_s, options)
      amount
    end

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

(see Proxy#load)

    # File lib/moneta/adapters/memcached/native.rb, line 34
    def load(key, options = {})
      value = @backend.get(key, false)
      if value
        expires = expires_value(options, nil)
        @backend.set(key, value, expires || 0, false) if expires != nil
        value
      end
    rescue ::Memcached::NotFound
    end

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

(see Proxy#store)

    # File lib/moneta/adapters/memcached/native.rb, line 45
    def store(key, value, options = {})
      # TTL must be Fixnum
      @backend.set(key, value, expires_value(options) || 0, false)
      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.