Class: Moneta::Adapters::Client

Inherits:
Moneta::Adapter show all
Defined in:
lib/moneta/adapters/client.rb

Overview

Moneta client backend

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

#[], #[]=, #decrement, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Object

Parameters:

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

Options Hash (options):

  • :backend (TCPSocket | UNIXSocket)

    an open socket to use

  • :port (Integer) — default: 9000

    TCP port

  • :host (String) — default: '127.0.0.1'

    Hostname

  • :socket (String)

    Unix socket file name as alternative to ‘:port` and `:host`



14
15
16
17
18
19
20
# File 'lib/moneta/adapters/client.rb', line 14

backend do |socket: nil, host: '127.0.0.1', port: 9000|
  if socket
    UNIXSocket.open(socket)
  else
    TCPSocket.open(host, port)
  end
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


60
61
62
63
64
# File 'lib/moneta/adapters/client.rb', line 60

def clear(options = {})
  write(:clear, options)
  read_msg
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



67
68
69
70
# File 'lib/moneta/adapters/client.rb', line 67

def close
  backend.close
  nil
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



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

def create(key, value, options = {})
  write(:create, key, value, options)
  read_msg
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
45
# File 'lib/moneta/adapters/client.rb', line 42

def delete(key, options = {})
  write(:delete, key, options)
  read_msg
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)

Raises:

  • (NotImplementedError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/moneta/adapters/client.rb', line 73

def each_key
  raise NotImplementedError, 'each_key is not supported' unless supports?(:each_key)
  return enum_for(:each_key) unless block_given?

  begin
    write(:each_key)
    yield_break = false

    loop do
      write('NEXT')

      # A StopIteration error will be raised by this call if the server
      # reached the end of the enumeration.  This will stop the loop
      # automatically.
      result = read_msg

      # yield_break will be true in the ensure block (below) if anything
      # happened during the yield to stop further enumeration.
      yield_break = true
      yield result
      yield_break = false
    end
  ensure
    write('BREAK') if yield_break
    read_msg # nil return from each_key
  end

  self
end

#featuresObject



104
105
106
107
108
109
110
# File 'lib/moneta/adapters/client.rb', line 104

def features
  @features ||=
    begin
      write(:features)
      read_msg.freeze
    end
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



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

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


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

def key?(key, options = {})
  write(:key?, key, options)
  read_msg
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



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

def load(key, options = {})
  write(:load, key, options)
  read_msg
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



35
36
37
38
39
# File 'lib/moneta/adapters/client.rb', line 35

def store(key, value, options = {})
  write(:store, key, value, options)
  read_msg
  value
end