Class: Moneta::Adapters::PStore

Inherits:
Moneta::Adapter show all
Includes:
NilValues
Defined in:
lib/moneta/adapters/pstore.rb

Overview

PStore backend

Direct Known Subclasses

YAML

Defined Under Namespace

Classes: TransactionError

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #close, #decrement, #features, #fetch, included, #supports?, #update

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

  • :threadsafe (Boolean)

    Makes the PStore thread-safe

  • :backend (::PStore)

    Use existing backend instance



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

def initialize(options = {})
  super
  @id = "Moneta::Adapters::PStore(#{object_id})"
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


80
81
82
83
84
85
86
87
# File 'lib/moneta/adapters/pstore.rb', line 80

def clear(options = {})
  transaction do
    backend.roots.each do |key|
      backend.delete(key)
    end
  end
  self
end

#create(key, value, options = {}) ⇒ Boolean

Note:

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.

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



68
69
70
71
72
73
74
75
76
77
# File 'lib/moneta/adapters/pstore.rb', line 68

def create(key, value, options = {})
  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

Parameters:

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

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



53
54
55
# File 'lib/moneta/adapters/pstore.rb', line 53

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

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


33
34
35
36
37
38
39
40
# File 'lib/moneta/adapters/pstore.rb', line 33

def each_key(&block)
  return enum_for(:each_key) { transaction(true) { backend.roots.size } } unless block_given?

  transaction(true) do
    backend.roots.each { |k| yield(k) }
  end
  self
end

#fetch_values(*keys, **options) ⇒ Object



94
95
96
# File 'lib/moneta/adapters/pstore.rb', line 94

def fetch_values(*keys, **options)
  transaction(true) { super }
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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



58
59
60
61
62
63
64
65
# File 'lib/moneta/adapters/pstore.rb', line 58

def increment(key, amount = 1, options = {})
  transaction do
    existing = backend[key]
    value = (existing == nil ? 0 : Integer(existing)) + amount
    backend[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 Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


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

def key?(key, options = {})
  transaction(true) { backend.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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



43
44
45
# File 'lib/moneta/adapters/pstore.rb', line 43

def load(key, options = {})
  transaction(true) { backend[key] }
end

#merge!(pairs, options = {}) ⇒ Object



102
103
104
# File 'lib/moneta/adapters/pstore.rb', line 102

def merge!(pairs, options = {})
  transaction { super }
end

#slice(*keys, **options) ⇒ Object



98
99
100
# File 'lib/moneta/adapters/pstore.rb', line 98

def slice(*keys, **options)
  transaction(true) { super }
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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



48
49
50
# File 'lib/moneta/adapters/pstore.rb', line 48

def store(key, value, options = {})
  transaction { backend[key] = value }
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



90
91
92
# File 'lib/moneta/adapters/pstore.rb', line 90

def values_at(*keys, **options)
  transaction(true) { super }
end