Class: Moneta::Adapters::ActiveRecord

### Home Classes Methods

### In Files

- lib/moneta/adapters/activerecord.rb

### Parent

Object

### Methods

- ::get
- ::new
- ::release
- #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::ActiveRecord

ActiveRecord as key/value stores @api public

### Attributes

table[R]

### Public Class Methods

get(options) click to toggle source

    # File lib/moneta/adapters/activerecord.rb, line 27
    def get(options)
      name = 'Table_' << options.inspect.gsub(/[^\w]+/) do
        $&.unpack('H2' * $&.bytesize).join.upcase
      end
      @table_mutex.synchronize do
        table =
          if const_defined?(name)
            const_get(name)
          else
            create(name, options)
          end
        @table_refcount[table] ||= 0
        @table_refcount[table] += 1
        table
      end
    end

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :table ('moneta') Table name @option options [Hash] :connection ActiveRecord connection configuration

    # File lib/moneta/adapters/activerecord.rb, line 82
    def initialize(options = {})
      @table = self.class.get(options)
    end

release(table) click to toggle source

    # File lib/moneta/adapters/activerecord.rb, line 18
    def release(table)
      @table_mutex.synchronize do
        if (@table_refcount[table] -= 1) <= 0
          remove_const(table.name.sub(/^.*::/, ''))
          @table_refcount.delete(table)
        end
      end
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/activerecord.rb, line 161
    def clear(options = {})
      @table.connection_pool.with_connection do
        @table.delete_all
      end
      self
    end

close() click to toggle source

(see Proxy#close)

    # File lib/moneta/adapters/activerecord.rb, line 169
    def close
      self.class.release(@table)
      @table = nil
    end

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

(see Proxy#create)

    # File lib/moneta/adapters/activerecord.rb, line 146
    def create(key, value, options = {})
      @table.connection_pool.with_connection do
        record = @table.new
        record.k = key
        record.v = value
        record.save
        true
      end
    rescue
      # FIXME: This catches too many errors
      # it should only catch a not-unique-exception
      false
    end

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

(see Proxy#delete)

    # File lib/moneta/adapters/activerecord.rb, line 115
    def delete(key, options = {})
      @table.connection_pool.with_connection do
        if record = @table.where(k: key).first
          record.destroy
          record.v
        end
      end
    end

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

(see Proxy#increment)

    # File lib/moneta/adapters/activerecord.rb, line 125
    def increment(key, amount = 1, options = {})
      @table.connection_pool.with_connection do
        @table.transaction do
          if record = @table.where(k: key).lock.first
            value = Utils.to_int(record.v) + amount
            record.v = value.to_s
            record.save
            value
          elsif create(key, amount.to_s, options)
            amount
          else
            raise 'Concurrent modification'
          end
        end
      end
    rescue
      tries ||= 0
      (tries += 1) < 10 ? retry : raise
    end

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

(see Proxy#key?)

    # File lib/moneta/adapters/activerecord.rb, line 87
    def key?(key, options = {})
      @table.connection_pool.with_connection do
        !@table.where(k: key).empty?
      end
    end

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

(see Proxy#load)

    # File lib/moneta/adapters/activerecord.rb, line 94
    def load(key, options = {})
      @table.connection_pool.with_connection do
        record = @table.select(:v).where(k: key).first
        record && record.v
      end
    end

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

(see Proxy#store)

    # File lib/moneta/adapters/activerecord.rb, line 102
    def store(key, value, options = {})
      @table.connection_pool.with_connection do
        record = @table.select(:k).where(k: key).first_or_initialize
        record.v = value
        record.save
        value
      end
    rescue
      tries ||= 0
      (tries += 1) < 10 ? retry : raise
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.