Class: Moneta::Adapters::PStore

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/pstore.rb

Overview

PStore backend

Direct Known Subclasses

YAML

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :file (String)

    PStore file

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/moneta/adapters/pstore.rb', line 12

def initialize(options = {})
  raise ArgumentError, 'Option :file is required' unless options[:file]
  FileUtils.mkpath(::File.dirname(options[:file]))
  @pstore = new_store(options)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


51
52
53
54
55
56
57
58
# File 'lib/moneta/adapters/pstore.rb', line 51

def clear(options = {})
  @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

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    current value



34
35
36
# File 'lib/moneta/adapters/pstore.rb', line 34

def delete(key, options = {})
  @pstore.transaction { @pstore.delete(key) }
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

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.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value from store



39
40
41
42
43
44
45
46
47
48
# File 'lib/moneta/adapters/pstore.rb', line 39

def increment(key, amount = 1, options = {})
  @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

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


19
20
21
# File 'lib/moneta/adapters/pstore.rb', line 19

def key?(key, options = {})
  @pstore.transaction(true) { @pstore.root?(key) }
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value



24
25
26
# File 'lib/moneta/adapters/pstore.rb', line 24

def load(key, options = {})
  @pstore.transaction(true) { @pstore[key] }
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Returns:

  • value



29
30
31
# File 'lib/moneta/adapters/pstore.rb', line 29

def store(key, value, options = {})
  @pstore.transaction { @pstore[key] = value }
end