Class: MudisClient

Inherits:
Object
  • Object
show all
Includes:
MudisIPCConfig
Defined in:
lib/mudis_client.rb

Overview

Thread-safe client for communicating with the MudisServer Automatically uses UNIX sockets on Linux/macOS and TCP on Windows

Constant Summary

Constants included from MudisIPCConfig

MudisIPCConfig::SOCKET_PATH, MudisIPCConfig::TCP_HOST, MudisIPCConfig::TCP_PORT

Instance Method Summary collapse

Methods included from MudisIPCConfig

use_tcp?

Constructor Details

#initializeMudisClient

Returns a new instance of MudisClient.



12
13
14
# File 'lib/mudis_client.rb', line 12

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#delete(key, namespace: nil) ⇒ Object

Delete a value from the Mudis server



66
67
68
# File 'lib/mudis_client.rb', line 66

def delete(key, namespace: nil)
  command("delete", key:, namespace:)
end

#exists?(key, namespace: nil) ⇒ Boolean

Check if a key exists in the Mudis server

Returns:

  • (Boolean)


71
72
73
# File 'lib/mudis_client.rb', line 71

def exists?(key, namespace: nil)
  command("exists", key:, namespace:)
end

#fetch(key, expires_in: nil, namespace: nil) ⇒ Object

Fetch a value, computing and storing it if not present



76
77
78
79
80
81
82
83
# File 'lib/mudis_client.rb', line 76

def fetch(key, expires_in: nil, namespace: nil)
  val = read(key, namespace:)
  return val if val

  new_val = yield
  write(key, new_val, expires_in:, namespace:)
  new_val
end

#metricsObject

Retrieve metrics from the Mudis server



86
87
88
# File 'lib/mudis_client.rb', line 86

def metrics
  command("metrics")
end

#open_connectionObject

Open a connection to the server (TCP or UNIX)



17
18
19
20
21
22
23
# File 'lib/mudis_client.rb', line 17

def open_connection
  if MudisIPCConfig.use_tcp?
    TCPSocket.new(TCP_HOST, TCP_PORT)
  else
    UNIXSocket.open(SOCKET_PATH)
  end
end

#read(key, namespace: nil) ⇒ Object

Read a value from the Mudis server



56
57
58
# File 'lib/mudis_client.rb', line 56

def read(key, namespace: nil)
  command("read", key:, namespace:)
end

#request(payload) ⇒ Object

Send a request to the MudisServer and return the response

Parameters:

  • payload (Hash)

    The request payload

Returns:

  • (Object)

    The response value from the server



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mudis_client.rb', line 28

def request(payload) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  @mutex.synchronize do
    sock = open_connection
    sock.puts(JSON.dump(payload))
    response = sock.gets
    sock.close

    return nil unless response

    res = JSON.parse(response, symbolize_names: true)
    raise res[:error] unless res[:ok]

    res[:value]
  rescue Errno::ENOENT, Errno::ECONNREFUSED
    warn "[MudisClient] Cannot connect to MudisServer. Is it running?"
    nil
  rescue JSON::ParserError
    warn "[MudisClient] Invalid JSON response from server"
    nil
  rescue IOError, SystemCallError => e
    warn "[MudisClient] Connection error: #{e.message}"
    nil
  end
end

#reset!Object

Reset the Mudis server cache state



96
97
98
# File 'lib/mudis_client.rb', line 96

def reset!
  command("reset")
end

#reset_metrics!Object

Reset metrics on the Mudis server



91
92
93
# File 'lib/mudis_client.rb', line 91

def reset_metrics!
  command("reset_metrics")
end

#write(key, value, expires_in: nil, namespace: nil) ⇒ Object

Write a value to the Mudis server



61
62
63
# File 'lib/mudis_client.rb', line 61

def write(key, value, expires_in: nil, namespace: nil)
  command("write", key:, value:, ttl: expires_in, namespace:)
end