Class: Moneta::Adapters::Client

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

Overview

Moneta client backend

Constant Summary

Constants included from Net

Net::DEFAULT_PORT

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Methods included from Net

#pack, #read, #write

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

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

Options Hash (options):

  • :port (Integer) — default: 9000

    TCP port

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

    Hostname

  • :file (String)

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



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

def initialize(options = {})
  @socket = options[:file] ? UNIXSocket.open(options[:file]) :
    TCPSocket.open(options[:host] || '127.0.0.1', options[:port] || DEFAULT_PORT)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


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

def clear(options = {})
  write(@socket, [:clear, options])
  read_result
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



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

def close
  @socket.close
  nil
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

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

Returns:

  • (Object)

    current value



40
41
42
43
# File 'lib/moneta/adapters/client.rb', line 40

def delete(key, options = {})
  write(@socket, [:delete, key, options])
  read_result
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



46
47
48
49
# File 'lib/moneta/adapters/client.rb', line 46

def increment(key, amount = 1, options = {})
  write(@socket, [:increment, key, amount, options])
  read_result
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/moneta/adapters/client.rb', line 21

def key?(key, options = {})
  write(@socket, [:key?, key, options])
  read_result
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

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

Returns:

  • (Object)

    value



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

def load(key, options = {})
  write(@socket, [:load, key, options])
  read_result
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

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

Returns:

  • value



33
34
35
36
37
# File 'lib/moneta/adapters/client.rb', line 33

def store(key, value, options = {})
  write(@socket, [:store, key, value, options])
  read_result
  value
end