Class: Moneta::Adapters::PStore
- Inherits:
-
Moneta::Adapter
- Object
- Moneta::Adapter
- Moneta::Adapters::PStore
- Includes:
- NilValues
- Defined in:
- lib/moneta/adapters/pstore.rb
Overview
PStore backend
Direct Known Subclasses
Defined Under Namespace
Classes: TransactionError
Instance Attribute Summary
Attributes inherited from Moneta::Adapter
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
- #fetch_values(*keys, **options) ⇒ Object
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ PStore
constructor
A new instance of PStore.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
- #merge!(pairs, options = {}) ⇒ Object
- #slice(*keys, **options) ⇒ Object
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods inherited from Moneta::Adapter
backend, backend_block, backend_required?
Methods included from Config
Methods included from Defaults
#[], #[]=, #close, #decrement, #features, #fetch, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ PStore
Returns a new instance of PStore.
22 23 24 25 |
# File 'lib/moneta/adapters/pstore.rb', line 22 def initialize( = {}) super @id = "Moneta::Adapters::PStore(#{object_id})" end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
80 81 82 83 84 85 86 87 |
# File 'lib/moneta/adapters/pstore.rb', line 80 def clear( = {}) transaction do backend.roots.each do |key| backend.delete(key) end end self end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/moneta/adapters/pstore.rb', line 68 def create(key, value, = {}) transaction do if backend.root?(key) false else backend[key] = value true end end end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
53 54 55 |
# File 'lib/moneta/adapters/pstore.rb', line 53 def delete(key, = {}) transaction { backend.delete(key) } end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
33 34 35 36 37 38 39 40 |
# File 'lib/moneta/adapters/pstore.rb', line 33 def each_key(&block) return enum_for(:each_key) { transaction(true) { backend.roots.size } } unless block_given? transaction(true) do backend.roots.each { |k| yield(k) } end self end |
#fetch_values(*keys, **options) ⇒ Object
94 95 96 |
# File 'lib/moneta/adapters/pstore.rb', line 94 def fetch_values(*keys, **) transaction(true) { super } 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.
58 59 60 61 62 63 64 65 |
# File 'lib/moneta/adapters/pstore.rb', line 58 def increment(key, amount = 1, = {}) transaction do existing = backend[key] value = (existing == nil ? 0 : Integer(existing)) + amount backend[key] = value.to_s value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
28 29 30 |
# File 'lib/moneta/adapters/pstore.rb', line 28 def key?(key, = {}) transaction(true) { backend.root?(key) } end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
43 44 45 |
# File 'lib/moneta/adapters/pstore.rb', line 43 def load(key, = {}) transaction(true) { backend[key] } end |
#merge!(pairs, options = {}) ⇒ Object
102 103 104 |
# File 'lib/moneta/adapters/pstore.rb', line 102 def merge!(pairs, = {}) transaction { super } end |
#slice(*keys, **options) ⇒ Object
98 99 100 |
# File 'lib/moneta/adapters/pstore.rb', line 98 def slice(*keys, **) transaction(true) { super } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
48 49 50 |
# File 'lib/moneta/adapters/pstore.rb', line 48 def store(key, value, = {}) transaction { backend[key] = value } end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.
Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.
90 91 92 |
# File 'lib/moneta/adapters/pstore.rb', line 90 def values_at(*keys, **) transaction(true) { super } end |