Class: Moneta::Adapters::PStore
- Inherits:
-
Object
- Object
- Moneta::Adapters::PStore
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/pstore.rb
Overview
PStore backend
Direct Known Subclasses
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 = {}) ⇒ PStore
constructor
A new instance of PStore.
-
#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 = {}) ⇒ PStore
Returns a new instance of PStore.
12 13 14 15 16 |
# File 'lib/moneta/adapters/pstore.rb', line 12 def initialize( = {}) raise ArgumentError, 'Option :file is required' unless [:file] FileUtils.mkpath(::File.dirname([:file])) @pstore = new_store() end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
51 52 53 54 55 56 57 58 |
# File 'lib/moneta/adapters/pstore.rb', line 51 def clear( = {}) @pstore.transaction do @pstore.roots.each do |key| @pstore.delete(key) end end self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
34 35 36 |
# File 'lib/moneta/adapters/pstore.rb', line 34 def delete(key, = {}) @pstore.transaction { @pstore.delete(key) } 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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/moneta/adapters/pstore.rb', line 39 def increment(key, amount = 1, = {}) @pstore.transaction do value = @pstore[key] intvalue = value.to_i raise 'Tried to increment non integer value' unless value == nil || intvalue.to_s == value.to_s intvalue += amount @pstore[key] = intvalue.to_s intvalue end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
19 20 21 |
# File 'lib/moneta/adapters/pstore.rb', line 19 def key?(key, = {}) @pstore.transaction(true) { @pstore.root?(key) } end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
24 25 26 |
# File 'lib/moneta/adapters/pstore.rb', line 24 def load(key, = {}) @pstore.transaction(true) { @pstore[key] } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
29 30 31 |
# File 'lib/moneta/adapters/pstore.rb', line 29 def store(key, value, = {}) @pstore.transaction { @pstore[key] = value } end |