Class: Moneta::Adapters::Sqlite

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

Overview

Sqlite3 backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IncrementSupport

included

Methods included from Defaults

#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #values_at

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

  • :busy_timeout (Integer) — default: 1000

    Sqlite timeout if database is busy

  • :backend (::Sqlite3::Database)

    Use existing backend instance


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#backendObject (readonly)


12
13
14
# File 'lib/moneta/adapters/sqlite.rb', line 12

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

67
68
69
70
# File 'lib/moneta/adapters/sqlite.rb', line 67

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

#closeObject

Explicitly close the store

Returns:

  • nil


84
85
86
87
88
# File 'lib/moneta/adapters/sqlite.rb', line 84

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

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


73
74
75
76
77
78
79
80
81
# 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 = {}) ⇒ 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 Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


55
56
57
58
59
# 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 = {}) ⇒ 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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store


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

def increment(key, amount = 1, options = {})
  @backend.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 Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

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

def key?(key, options = {})
  @exists.execute!(key).first.first.to_i == 1
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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


43
44
45
46
# 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 = {}) ⇒ Object

Store value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


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

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