Class: Moneta::Adapters::Sequel

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

Overview

Sequel backend

Instance Method Summary collapse

Methods included from Defaults

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Sequel

Returns a new instance of Sequel.

Parameters:

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

Options Hash (options):

  • :db (String)

    Sequel database

  • :table (String/Symbol) — default: :moneta

    Table name

  • All (Object)

    other options passed to ‘Sequel#connect`

Raises:

  • (ArgumentError)


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

def initialize(options = {})
  raise ArgumentError, 'Option :db is required' unless db = options.delete(:db)
  table = options.delete(:table) || :moneta
  @db = ::Sequel.connect(db, options)
  @db.create_table?(table) do
    String :k, :null => false, :primary_key => true
    String :v
  end
  @table = @db[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: {})


75
76
77
78
# File 'lib/moneta/adapters/sequel.rb', line 75

def clear(options = {})
  @table.delete
  self
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



65
66
67
68
69
70
71
72
# File 'lib/moneta/adapters/sequel.rb', line 65

def delete(key, options = {})
  @db.transaction do
    if value = load(key, options)
      @table.filter(:k => key).delete
      value
    end
  end
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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/moneta/adapters/sequel.rb', line 50

def increment(key, amount = 1, options = {})
  @db.transaction do
    locked_table = @table.for_update
    if record = locked_table[:k => key]
      value = convert_for_increment(record[:v]) + amount
      locked_table.update(:k => key, :v => value.to_s)
      value
    else
      locked_table.insert(:k => key, :v => amount.to_s)
      amount
    end
  end
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/sequel.rb', line 27

def key?(key, options = {})
  @table[:k => key] != nil
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/sequel.rb', line 32

def load(key, options = {})
  record = @table[:k => key]
  record && record[:v]
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
42
43
44
45
46
47
# File 'lib/moneta/adapters/sequel.rb', line 38

def store(key, value, options = {})
  @db.transaction do
    if key?(key, options)
      @table.update(:k => key, :v => value)
    else
      @table.insert(:k => key, :v => value)
    end
    value
  end
end