Class: Moneta::Adapters::Sequel
- Inherits:
-
Object
- Object
- Moneta::Adapters::Sequel
- 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
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Object
Store value with key.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ Sequel
constructor
A new instance of Sequel.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/moneta/adapters/sequel.rb', line 22 def initialize( = {}) table = (.delete(:table) || :moneta).to_sym @backend = [:backend] || begin raise ArgumentError, 'Option :db is required' unless db = .delete(:db) ::Sequel.connect(db, ) end @backend.create_table?(table) do String :k, null: false, primary_key: true File :v end @table = @backend[table] end |
Instance Attribute Details
#backend ⇒ Object (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
96 97 98 99 |
# File 'lib/moneta/adapters/sequel.rb', line 96 def clear( = {}) @table.delete self end |
#close ⇒ Object
Explicitly close the store
102 103 104 105 |
# File 'lib/moneta/adapters/sequel.rb', line 102 def close @backend.disconnect nil end |
#create(key, value, options = {}) ⇒ Object
Store value with key
61 62 63 64 65 66 |
# File 'lib/moneta/adapters/sequel.rb', line 61 def create(key, value, = {}) @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
88 89 90 91 92 93 |
# File 'lib/moneta/adapters/sequel.rb', line 88 def delete(key, = {}) if value = load(key, ) @table.filter(k: key).delete value end end |
#increment(key, amount = 1, options = {}) ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/moneta/adapters/sequel.rb', line 69 def increment(key, amount = 1, = {}) @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
37 38 39 |
# File 'lib/moneta/adapters/sequel.rb', line 37 def key?(key, = {}) @table[k: key] != nil end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
42 43 44 45 |
# File 'lib/moneta/adapters/sequel.rb', line 42 def load(key, = {}) record = @table[k: key] record && record[:v] end |
#store(key, value, options = {}) ⇒ Object
Store value with key
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/moneta/adapters/sequel.rb', line 48 def store(key, value, = {}) 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 |