Class: Moneta::Adapters::Sqlite

Inherits:
Object
  • Object
show all
Includes:
Defaults, IncrementSupport
Defined in:
lib/moneta/adapters/sqlite.rb

Overview

Sqlite3 backend

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Sqlite

Returns a new instance of Sqlite

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :file (String)

    Database file

  • :table (String) — default: 'moneta'

    Table name

Raises:

  • (ArgumentError)

14
15
16
17
18
19
20
21
22
23
24
# File 'lib/moneta/adapters/sqlite.rb', line 14

def initialize(options = {})
  raise ArgumentError, 'Option :file is required' unless options[:file]
  table = options[:table] || 'moneta'
  @db = ::SQLite3::Database.new(options[:file])
  @db.execute("create table if not exists #{table} (k blob not null primary key, v blob)")
  @stmts =
    [@select = @db.prepare("select v from #{table} where k = ?"),
     @replace = @db.prepare("replace into #{table} values (?, ?)"),
     @delete = @db.prepare("delete from #{table} where k = ?"),
     @clear = @db.prepare("delete from #{table}")]
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})

56
57
58
59
# File 'lib/moneta/adapters/sqlite.rb', line 56

def clear(options = {})
  @clear.execute!
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil


62
63
64
65
66
# File 'lib/moneta/adapters/sqlite.rb', line 62

def close
  @stmts.each {|s| s.close }
  @db.close
  nil
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


44
45
46
47
48
# File 'lib/moneta/adapters/sqlite.rb', line 44

def delete(key, options = {})
  value = load(key, options)
  @delete.execute!(key)
  value
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value from store


51
52
53
# File 'lib/moneta/adapters/sqlite.rb', line 51

def increment(key, amount = 1, options = {})
  @db.transaction(:exclusive) { return super }
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

27
28
29
# File 'lib/moneta/adapters/sqlite.rb', line 27

def key?(key, options = {})
  !@select.execute!(key).empty?
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn't exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


32
33
34
35
# File 'lib/moneta/adapters/sqlite.rb', line 32

def load(key, options = {})
  rows = @select.execute!(key)
  rows.empty? ? nil : rows.first.first
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


38
39
40
41
# File 'lib/moneta/adapters/sqlite.rb', line 38

def store(key, value, options = {})
  @replace.execute!(key, value)
  value
end