Class: Moneta::Adapters::PStore

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

Overview

PStore backend

Direct Known Subclasses

YAML

Defined Under Namespace

Classes: TransactionError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

  • :backend (::PStore)

    Use existing backend instance



18
19
20
21
22
23
24
25
26
27
# File 'lib/moneta/adapters/pstore.rb', line 18

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

  @id = "Moneta::Adapters::PStore(#{object_id})"
end

Instance Attribute Details

#backendObject (readonly)



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

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


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

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



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

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



55
56
57
# File 'lib/moneta/adapters/pstore.rb', line 55

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)


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

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



96
97
98
# File 'lib/moneta/adapters/pstore.rb', line 96

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



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

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)


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

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



45
46
47
# File 'lib/moneta/adapters/pstore.rb', line 45

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

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



104
105
106
# File 'lib/moneta/adapters/pstore.rb', line 104

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

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



100
101
102
# File 'lib/moneta/adapters/pstore.rb', line 100

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



50
51
52
# File 'lib/moneta/adapters/pstore.rb', line 50

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



92
93
94
# File 'lib/moneta/adapters/pstore.rb', line 92

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