Class: Moneta::Adapters::PStore
- Inherits:
-
Object
- Object
- Moneta::Adapters::PStore
- Includes:
- Defaults, IncrementSupport
- 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.
13 14 15 16 17 |
# File 'lib/moneta/adapters/pstore.rb', line 13 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
49 50 51 52 53 54 55 56 |
# File 'lib/moneta/adapters/pstore.rb', line 49 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
35 36 37 |
# File 'lib/moneta/adapters/pstore.rb', line 35 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.
40 41 42 43 44 45 46 |
# File 'lib/moneta/adapters/pstore.rb', line 40 def increment(key, amount = 1, = {}) @pstore.transaction do value = convert_for_increment(@pstore[key]) + amount @pstore[key] = value.to_s value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
20 21 22 |
# File 'lib/moneta/adapters/pstore.rb', line 20 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
25 26 27 |
# File 'lib/moneta/adapters/pstore.rb', line 25 def load(key, = {}) @pstore.transaction(true) { @pstore[key] } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
30 31 32 |
# File 'lib/moneta/adapters/pstore.rb', line 30 def store(key, value, = {}) @pstore.transaction { @pstore[key] = value } end |