Class: Moneta::Adapters::Sqlite

### Home Classes Methods

### In Files

- lib/moneta/adapters/sqlite.rb

### Parent

Object

### Methods

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

### Included Modules

- Moneta::Defaults
- Moneta::IncrementSupport

### 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::Mongo
- 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::Sqlite

Sqlite3 backend @api public

### Attributes

backend[R]

### Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :file Database file @option options [String] :table ('moneta') Table name @option options [Fixnum] :busy_timeout (1000) Sqlite timeout if database is busy @option options [::Sqlite3::Database] :backend Use existing backend instance

    # File lib/moneta/adapters/sqlite.rb, line 19
    def initialize(options = {})
      table = options[:table] || 'moneta'
      @backend = options[:backend] ||
        begin
          raise ArgumentError, 'Option :file is required' unless options[:file]
          ::SQLite3::Database.new(options[:file])
        end
      @backend.busy_timeout(options[:busy_timeout] || 1000)
      @backend.execute("create table if not exists #{table} (k blob not null primary key, v blob)")
      @stmts =
        [@exists  = @backend.prepare("select exists(select 1 from #{table} where k = ?)"),
         @select  = @backend.prepare("select v from #{table} where k = ?"),
         @replace = @backend.prepare("replace into #{table} values (?, ?)"),
         @delete  = @backend.prepare("delete from #{table} where k = ?"),
         @clear   = @backend.prepare("delete from #{table}"),
         @create  = @backend.prepare("insert into #{table} values (?, ?)")]
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/sqlite.rb, line 67
    def clear(options = {})
      @clear.execute!
      self
    end

close() click to toggle source

(see Proxy#close)

    # File lib/moneta/adapters/sqlite.rb, line 84
    def close
      @stmts.each {|s| s.close }
      @backend.close
      nil
    end

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

(see Default#create)

    # File lib/moneta/adapters/sqlite.rb, line 73
    def create(key, value, options = {})
      @create.execute!(key,value)
      true
    rescue SQLite3::ConstraintException
      # If you know a better way to detect whether an insert-ignore
      # suceeded, please tell me.
      @create.reset!
      false
    end

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

(see Proxy#delete)

    # File lib/moneta/adapters/sqlite.rb, line 55
    def delete(key, options = {})
      value = load(key, options)
      @delete.execute!(key)
      value
    end

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

(see Proxy#increment)

    # File lib/moneta/adapters/sqlite.rb, line 62
    def increment(key, amount = 1, options = {})
      @backend.transaction(:exclusive) { return super }
    end

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

(see Proxy#key?)

    # File lib/moneta/adapters/sqlite.rb, line 38
    def key?(key, options = {})
      @exists.execute!(key).first.first.to_i == 1
    end

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

(see Proxy#load)

    # File lib/moneta/adapters/sqlite.rb, line 43
    def load(key, options = {})
      rows = @select.execute!(key)
      rows.empty? ? nil : rows.first.first
    end

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

(see Proxy#store)

    # File lib/moneta/adapters/sqlite.rb, line 49
    def store(key, value, options = {})
      @replace.execute!(key, value)
      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.