Class: Moneta::Adapters::Sequel
- Inherits:
-
Object
- Object
- Moneta::Adapters::Sequel
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/sequel.rb
Overview
Sequel backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#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
#[], #[]=, #close, #decrement, #fetch
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Sequel
Returns a new instance of Sequel.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/moneta/adapters/sequel.rb', line 14 def initialize( = {}) raise ArgumentError, 'Option :db is required' unless db = .delete(:db) table = .delete(:table) || :moneta @db = ::Sequel.connect(db, ) @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
77 78 79 80 |
# File 'lib/moneta/adapters/sequel.rb', line 77 def clear( = {}) @table.delete self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
67 68 69 70 71 72 73 74 |
# File 'lib/moneta/adapters/sequel.rb', line 67 def delete(key, = {}) @db.transaction do if value = load(key, ) @table.filter(:k => key).delete value end 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moneta/adapters/sequel.rb', line 49 def increment(key, amount = 1, = {}) @db.transaction do locked_table = @table.for_update if record = locked_table[:k => key] value = record[:v] intvalue = value.to_i raise 'Tried to increment non integer value' unless value == nil || intvalue.to_s == value.to_s intvalue += amount locked_table.update(:k => key, :v => intvalue.to_s) intvalue else locked_table.insert(:k => key, :v => amount.to_s) amount end end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
26 27 28 |
# File 'lib/moneta/adapters/sequel.rb', line 26 def key?(key, = {}) @table[:k => key] != nil end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
31 32 33 34 |
# File 'lib/moneta/adapters/sequel.rb', line 31 def load(key, = {}) record = @table[:k => key] record && record[:v] end |
#store(key, value, options = {}) ⇒ Object
Store value with key
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/moneta/adapters/sequel.rb', line 37 def store(key, value, = {}) @db.transaction do if key?(key, ) @table.update(:k => key, :v => value) else @table.insert(:k => key, :v => value) end value end end |