Class: Moneta::Adapters::Sequel

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

Overview

Sequel backend

Constant Summary collapse

UniqueConstraintViolation =

Sequel::UniqueConstraintViolation is defined since sequel 3.44.0 older versions raise a Sequel::DatabaseError.

defined?(::Sequel::UniqueConstraintViolation) ? ::Sequel::UniqueConstraintViolation : ::Sequel::DatabaseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #supports?

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Sequel

Returns a new instance of Sequel

Options Hash (options):

  • :db (String)

    Sequel database

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

    Table name

  • :extensions (Array) — default: []

    List of Sequel extensions

  • :connection_validation_timeout (Integer) — default: nil

    Sequel connection_validation_timeout

  • All (Object)

    other options passed to `Sequel#connect`

  • :backend (Sequel connection)

    Use existing backend instance

Raises:

  • (ArgumentError)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moneta/adapters/sequel.rb', line 24

def initialize(options = {})
  table = (options.delete(:table) || :moneta).to_sym
  extensions = options.delete(:extensions) || []
  raise ArgumentError, 'Option :extensions must be an Array' unless extensions.is_a?(Array)
  connection_validation_timeout = options.delete(:connection_validation_timeout)
  @backend = options[:backend] ||
    begin
      raise ArgumentError, 'Option :db is required' unless db = options.delete(:db)
      ::Sequel.connect(db, options)
    end
  extensions.each do |extension|
    @backend.extension(extension.to_sym)
  end
  @backend.pool.connection_validation_timeout = connection_validation_timeout if connection_validation_timeout
  @backend.create_table?(table) do
    String :k, null: false, primary_key: true
    File :v
  end
  @table = @backend[table]
end

Instance Attribute Details

#backendObject (readonly)


15
16
17
# File 'lib/moneta/adapters/sequel.rb', line 15

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store


105
106
107
108
# File 'lib/moneta/adapters/sequel.rb', line 105

def clear(options = {})
  @table.delete
  self
end

#closeObject

Explicitly close the store


111
112
113
114
# File 'lib/moneta/adapters/sequel.rb', line 111

def close
  @backend.disconnect
  nil
end

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

Store value with key

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


70
71
72
73
74
75
# File 'lib/moneta/adapters/sequel.rb', line 70

def create(key, value, options = {})
  @table.insert(k: key, v: blob(value))
  true
rescue UniqueConstraintViolation
  false
end

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

Delete the key from the store and return the current value

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


97
98
99
100
101
102
# File 'lib/moneta/adapters/sequel.rb', line 97

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

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/moneta/adapters/sequel.rb', line 78

def increment(key, amount = 1, options = {})
  @backend.transaction do
    locked_table = @table.for_update
    if record = locked_table[k: key]
      value = Utils.to_int(record[:v]) + amount
      locked_table.where(k: key).update(v: blob(value.to_s))
      value
    else
      locked_table.insert(k: key, v: blob(amount.to_s))
      amount
    end
  end
rescue ::Sequel::DatabaseError
  # Concurrent modification might throw a bunch of different errors
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
end

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

Exists the value with key

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


46
47
48
# File 'lib/moneta/adapters/sequel.rb', line 46

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

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


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

def load(key, options = {})
  record = @table[k: key]
  record && record[:v]
end

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

Store value with key

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


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/moneta/adapters/sequel.rb', line 57

def store(key, value, options = {})
  begin
    @table.insert(k: key, v: blob(value))
  rescue UniqueConstraintViolation
    @table.where(k: key).update(v: blob(value))
  end
  value
rescue ::Sequel::DatabaseError
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
end