Class: Moneta::Adapters::Sequel

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

Overview

Sequel backend

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.

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`

  • :backend (Sequel connection)

    Use existing backend instance



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moneta/adapters/sequel.rb', line 18

def initialize(options = {})
  table = (options.delete(:table) || :moneta).to_sym
  @backend = options[:backend] ||
    begin
      raise ArgumentError, 'Option :db is required' unless db = options.delete(:db)
      ::Sequel.connect(db, options)
    end
  @backend.create_table?(table) do
    String :k, :null => false, :primary_key => true
    String :v
  end
  @table = @backend[table]
end

Instance Attribute Details

#backendObject (readonly)



11
12
13
# File 'lib/moneta/adapters/sequel.rb', line 11

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: {})


95
96
97
98
# File 'lib/moneta/adapters/sequel.rb', line 95

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

#closeObject

Explicitly close the store

Returns:

  • nil



101
102
103
104
# File 'lib/moneta/adapters/sequel.rb', line 101

def close
  @backend.disconnect
  nil
end

#create(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



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

def create(key, value, options = {})
  @table.insert(:k => key, :v => value)
  true
rescue ::Sequel::DatabaseError
  # FIXME: This catches too many errors
  # it should only catch a not-unique-exception
  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



87
88
89
90
91
92
# File 'lib/moneta/adapters/sequel.rb', line 87

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.

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/moneta/adapters/sequel.rb', line 67

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 => value.to_s)
      value
    else
      locked_table.insert(:k => key, :v => amount.to_s)
      amount
    end
  end
rescue ::Sequel::DatabaseError
  # FIXME: This catches too many errors
  # it should only catch a not-unique-exception
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
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)


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

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 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:

  • (Object)

    value



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

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 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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moneta/adapters/sequel.rb', line 44

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