Class: Moneta::Adapters::PStore
- Inherits:
-
Object
- Object
- Moneta::Adapters::PStore
- Defined in:
- lib/moneta/adapters/pstore.rb
Overview
PStore backend
Direct Known Subclasses
Defined Under Namespace
Classes: TransactionError
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
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 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.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/moneta/adapters/pstore.rb', line 18 def initialize( = {}) @backend = [:backend] || begin raise ArgumentError, 'Option :file is required' unless [:file] FileUtils.mkpath(::File.dirname([:file])) new_store() end @id = "Moneta::Adapters::PStore(#{object_id})" end |
Instance Attribute Details
#backend ⇒ Object (readonly)
13 14 15 |
# File 'lib/moneta/adapters/pstore.rb', line 13 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
82 83 84 85 86 87 88 89 |
# File 'lib/moneta/adapters/pstore.rb', line 82 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.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/moneta/adapters/pstore.rb', line 70 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
55 56 57 |
# File 'lib/moneta/adapters/pstore.rb', line 55 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.
35 36 37 38 39 40 41 42 |
# File 'lib/moneta/adapters/pstore.rb', line 35 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
96 97 98 |
# File 'lib/moneta/adapters/pstore.rb', line 96 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.
60 61 62 63 64 65 66 67 |
# File 'lib/moneta/adapters/pstore.rb', line 60 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
30 31 32 |
# File 'lib/moneta/adapters/pstore.rb', line 30 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
45 46 47 |
# File 'lib/moneta/adapters/pstore.rb', line 45 def load(key, = {}) transaction(true) { @backend[key] } end |
#merge!(pairs, options = {}) ⇒ Object
104 105 106 |
# File 'lib/moneta/adapters/pstore.rb', line 104 def merge!(pairs, = {}) transaction { super } end |
#slice(*keys, **options) ⇒ Object
100 101 102 |
# File 'lib/moneta/adapters/pstore.rb', line 100 def slice(*keys, **) transaction(true) { super } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
50 51 52 |
# File 'lib/moneta/adapters/pstore.rb', line 50 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.
92 93 94 |
# File 'lib/moneta/adapters/pstore.rb', line 92 def values_at(*keys, **) transaction(true) { super } end |