Class: Ask::State::Adapter
- Inherits:
-
Object
- Object
- Ask::State::Adapter
- Defined in:
- lib/ask/state.rb
Overview
Abstract base class for state backends. Subclasses must implement all methods.
Class Method Summary collapse
-
.glob_to_like(pattern) ⇒ String
Convert a glob pattern (*, ?) to a SQL LIKE pattern.
-
.glob_to_regex(pattern) ⇒ Regexp
Convert a glob pattern (*, ?) to a Regexp.
Instance Method Summary collapse
-
#acquire_lock(key, ttl: 10) ⇒ Lock?
Acquire a lock for a key.
-
#clear ⇒ Object
Remove all keys from the store (key-value only by default).
-
#close ⇒ Object
Optional: called when the adapter is no longer needed.
- #delete(key) ⇒ Object
-
#dequeue(queue) ⇒ QueueEntry?
Pop the next item from a named queue.
-
#enqueue(queue, value) ⇒ QueueEntry
Push an item onto a named queue.
-
#exists?(key) ⇒ Boolean
Check if a key exists and has not expired.
-
#get(key) ⇒ Object?
The stored value, or nil if not found.
-
#keys(pattern: nil) ⇒ Array<String>
Return all non-expired keys, optionally filtered by a glob pattern.
-
#list_append(key, value, max_length: nil) ⇒ Object
Append a value to an ordered list.
-
#list_range(key, start = 0, stop = -1)) ⇒ Array<Object>
Return a slice of the list.
-
#list_remove(key, value) ⇒ Integer
Remove all occurrences of a value from a list.
-
#queue_depth(queue) ⇒ Integer
The number of items in the queue.
-
#release_lock(key, lock) ⇒ Boolean
Release a lock.
- #set(key, value, ttl: nil) ⇒ Object
-
#set_if_not_exists(key, value, ttl: nil) ⇒ Boolean
Atomically set a value only if the key does not already exist.
Class Method Details
.glob_to_like(pattern) ⇒ String
Convert a glob pattern (*, ?) to a SQL LIKE pattern.
175 176 177 |
# File 'lib/ask/state.rb', line 175 def self.glob_to_like(pattern) pattern.gsub("*", "%").gsub("?", "_") end |
.glob_to_regex(pattern) ⇒ Regexp
Convert a glob pattern (*, ?) to a Regexp.
182 183 184 185 |
# File 'lib/ask/state.rb', line 182 def self.glob_to_regex(pattern) escaped = Regexp.escape(pattern) Regexp.new("\\A#{escaped.gsub("\\*", ".*").gsub("\\?", ".")}\\z") end |
Instance Method Details
#acquire_lock(key, ttl: 10) ⇒ Lock?
Acquire a lock for a key. Returns nil if the lock is held by another owner.
101 102 103 |
# File 'lib/ask/state.rb', line 101 def acquire_lock(key, ttl: 10) raise NotImplementedError end |
#clear ⇒ Object
Remove all keys from the store (key-value only by default). Subclasses should override to also clear locks, queues, and lists.
68 69 70 |
# File 'lib/ask/state.rb', line 68 def clear raise NotImplementedError end |
#close ⇒ Object
Optional: called when the adapter is no longer needed.
166 167 168 |
# File 'lib/ask/state.rb', line 166 def close # no-op by default end |
#delete(key) ⇒ Object
62 63 64 |
# File 'lib/ask/state.rb', line 62 def delete(key) raise NotImplementedError end |
#dequeue(queue) ⇒ QueueEntry?
Pop the next item from a named queue.
126 127 128 |
# File 'lib/ask/state.rb', line 126 def dequeue(queue) raise NotImplementedError end |
#enqueue(queue, value) ⇒ QueueEntry
Push an item onto a named queue.
119 120 121 |
# File 'lib/ask/state.rb', line 119 def enqueue(queue, value) raise NotImplementedError end |
#exists?(key) ⇒ Boolean
Check if a key exists and has not expired.
75 76 77 |
# File 'lib/ask/state.rb', line 75 def exists?(key) !get(key).nil? end |
#get(key) ⇒ Object?
Returns the stored value, or nil if not found.
50 51 52 |
# File 'lib/ask/state.rb', line 50 def get(key) raise NotImplementedError end |
#keys(pattern: nil) ⇒ Array<String>
Return all non-expired keys, optionally filtered by a glob pattern. Glob syntax: * matches any sequence, ? matches any single character.
83 84 85 |
# File 'lib/ask/state.rb', line 83 def keys(pattern: nil) raise NotImplementedError end |
#list_append(key, value, max_length: nil) ⇒ Object
Append a value to an ordered list. Trims to max_length (keeps newest).
142 143 144 |
# File 'lib/ask/state.rb', line 142 def list_append(key, value, max_length: nil) raise NotImplementedError end |
#list_range(key, start = 0, stop = -1)) ⇒ Array<Object>
Return a slice of the list.
151 152 153 |
# File 'lib/ask/state.rb', line 151 def list_range(key, start = 0, stop = -1) raise NotImplementedError end |
#list_remove(key, value) ⇒ Integer
Remove all occurrences of a value from a list.
159 160 161 |
# File 'lib/ask/state.rb', line 159 def list_remove(key, value) raise NotImplementedError end |
#queue_depth(queue) ⇒ Integer
Returns the number of items in the queue.
132 133 134 |
# File 'lib/ask/state.rb', line 132 def queue_depth(queue) raise NotImplementedError end |
#release_lock(key, lock) ⇒ Boolean
Release a lock. Only the lock owner can release it.
109 110 111 |
# File 'lib/ask/state.rb', line 109 def release_lock(key, lock) raise NotImplementedError end |
#set(key, value, ttl: nil) ⇒ Object
57 58 59 |
# File 'lib/ask/state.rb', line 57 def set(key, value, ttl: nil) raise NotImplementedError end |
#set_if_not_exists(key, value, ttl: nil) ⇒ Boolean
Atomically set a value only if the key does not already exist.
91 92 93 |
# File 'lib/ask/state.rb', line 91 def set_if_not_exists(key, value, ttl: nil) raise NotImplementedError end |