Class: Moneta::Adapters::PStore

Inherits:
Object
  • Object
show all
Includes:
Defaults, IncrementSupport
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)

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

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: {})

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

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: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


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

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


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

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

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

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

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: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


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

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: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


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

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