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 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.
-
#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, #features, #fetch, included, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ PStore
Returns a new instance of PStore.
16 17 18 19 20 21 22 23 |
# File 'lib/moneta/adapters/pstore.rb', line 16 def initialize( = {}) @backend = [:backend] || begin raise ArgumentError, 'Option :file is required' unless [:file] FileUtils.mkpath(::File.dirname([:file])) new_store() end end |
Instance Attribute Details
#backend ⇒ Object (readonly)
11 12 13 |
# File 'lib/moneta/adapters/pstore.rb', line 11 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
67 68 69 70 71 72 73 74 |
# File 'lib/moneta/adapters/pstore.rb', line 67 def clear( = {}) @backend.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.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moneta/adapters/pstore.rb', line 55 def create(key, value, = {}) @backend.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
41 42 43 |
# File 'lib/moneta/adapters/pstore.rb', line 41 def delete(key, = {}) @backend.transaction { @backend.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.
46 47 48 49 50 51 52 |
# File 'lib/moneta/adapters/pstore.rb', line 46 def increment(key, amount = 1, = {}) @backend.transaction do value = Utils.to_int(@backend[key]) + amount @backend[key] = value.to_s value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
26 27 28 |
# File 'lib/moneta/adapters/pstore.rb', line 26 def key?(key, = {}) @backend.transaction(true) { @backend.root?(key) } end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
31 32 33 |
# File 'lib/moneta/adapters/pstore.rb', line 31 def load(key, = {}) @backend.transaction(true) { @backend[key] } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
36 37 38 |
# File 'lib/moneta/adapters/pstore.rb', line 36 def store(key, value, = {}) @backend.transaction { @backend[key] = value } end |