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 Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

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

Options Hash (options):

  • :file (String)

    PStore file

  • :backend (::PStore)

    Use existing backend instance


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

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
end

Instance Attribute Details

#backendObject (readonly)


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

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

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

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


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

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

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


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

def delete(key, options = {})
  @backend.transaction { @backend.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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store


47
48
49
50
51
52
53
# File 'lib/moneta/adapters/pstore.rb', line 47

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

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)

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

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


32
33
34
# File 'lib/moneta/adapters/pstore.rb', line 32

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


37
38
39
# File 'lib/moneta/adapters/pstore.rb', line 37

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