Class: Moneta::Adapters::Sequel

### Home Classes Methods

### In Files

- lib/moneta/adapters/sequel.rb

### Parent

Object

### Methods

- ::new
- #clear
- #close
- #create
- #delete
- #increment
- #key?
- #load
- #store

### Included Modules

- Moneta::Defaults

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

Sequel backend @api public

### Constants

UniqueConstraintViolation

  Sequel::UniqueConstraintViolation is defined since sequel 3.44.0 older versions raise a Sequel::DatabaseError.

### Attributes

backend[R]

### Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :db Sequel database @option options [String/Symbol] :table (:moneta) Table name @option options All other options passed to `Sequel#connect` @option options [Sequel connection] :backend Use existing backend instance

    # File lib/moneta/adapters/sequel.rb, line 22
    def initialize(options = {})
      table = (options.delete(:table) || :moneta).to_sym
      @backend = options[:backend] ||
        begin
          raise ArgumentError, 'Option :db is required' unless db = options.delete(:db)
          ::Sequel.connect(db, options)
        end
      @backend.create_table?(table) do
        String :k, null: false, primary_key: true
        Blob :v
      end
      @table = @backend[table]
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/sequel.rb, line 96
    def clear(options = {})
      @table.delete
      self
    end

close() click to toggle source

(see Proxy#close)

    # File lib/moneta/adapters/sequel.rb, line 102
    def close
      @backend.disconnect
      nil
    end

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

(see Proxy#store)

    # File lib/moneta/adapters/sequel.rb, line 61
    def create(key, value, options = {})
      @table.insert(k: key, v: blob(value))
      true
    rescue UniqueConstraintViolation
      false
    end

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

(see Proxy#delete)

    # File lib/moneta/adapters/sequel.rb, line 88
    def delete(key, options = {})
      if value = load(key, options)
        @table.filter(k: key).delete
        value
      end
    end

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

(see Proxy#increment)

    # File lib/moneta/adapters/sequel.rb, line 69
    def increment(key, amount = 1, options = {})
      @backend.transaction do
        locked_table = @table.for_update
        if record = locked_table[k: key]
          value = Utils.to_int(record[:v]) + amount
          locked_table.where(k: key).update(v: blob(value.to_s))
          value
        else
          locked_table.insert(k: key, v: blob(amount.to_s))
          amount
        end
      end
    rescue ::Sequel::DatabaseError
      # Concurrent modification might throw a bunch of different errors
      tries ||= 0
      (tries += 1) < 10 ? retry : raise
    end

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

(see Proxy#key?)

    # File lib/moneta/adapters/sequel.rb, line 37
    def key?(key, options = {})
      @table[k: key] != nil
    end

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

(see Proxy#load)

    # File lib/moneta/adapters/sequel.rb, line 42
    def load(key, options = {})
      record = @table[k: key]
      record && record[:v]
    end

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

(see Proxy#store)

    # File lib/moneta/adapters/sequel.rb, line 48
    def store(key, value, options = {})
      begin
        @table.insert(k: key, v: blob(value))
      rescue UniqueConstraintViolation
        @table.where(k: key).update(v: blob(value))
      end
      value
    rescue ::Sequel::DatabaseError
      tries ||= 0
      (tries += 1) < 10 ? retry : raise
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.