Class: MudisClient
- Inherits:
-
Object
- Object
- MudisClient
- 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::DEFAULT_RETRIES, MudisIPCConfig::DEFAULT_TIMEOUT, MudisIPCConfig::SOCKET_PATH, MudisIPCConfig::TCP_HOST, MudisIPCConfig::TCP_PORT
Instance Method Summary collapse
-
#all_keys ⇒ Object
Return all keys.
-
#clear_namespace(namespace:) ⇒ Object
Clear keys in a namespace.
-
#current_memory_bytes ⇒ Object
Return current memory usage.
-
#delete(key, namespace: nil) ⇒ Object
Delete a value from the Mudis server.
-
#exists?(key, namespace: nil) ⇒ Boolean
Check if a key exists in the Mudis server.
-
#fetch(key, expires_in: nil, namespace: nil) ⇒ Object
Fetch a value, computing and storing it if not present.
-
#initialize ⇒ MudisClient
constructor
A new instance of MudisClient.
-
#inspect(key, namespace: nil) ⇒ Object
Inspect metadata for a key.
-
#keys(namespace:) ⇒ Object
Return keys for a namespace.
-
#least_touched(limit = 10) ⇒ Object
Return least touched keys.
-
#max_memory_bytes ⇒ Object
Return max memory configured.
-
#metrics ⇒ Object
Retrieve metrics from the Mudis server.
-
#open_connection ⇒ Object
Open a connection to the server (TCP or UNIX).
-
#read(key, namespace: nil) ⇒ Object
Read a value from the Mudis server.
-
#request(payload) ⇒ Object
Send a request to the MudisServer and return the response.
-
#write(key, value, expires_in: nil, namespace: nil) ⇒ Object
Write a value to the Mudis server.
Methods included from MudisIPCConfig
Constructor Details
#initialize ⇒ MudisClient
Returns a new instance of MudisClient.
13 14 15 |
# File 'lib/mudis_client.rb', line 13 def initialize @mutex = Mutex.new end |
Instance Method Details
#all_keys ⇒ Object
Return all keys
120 121 122 |
# File 'lib/mudis_client.rb', line 120 def all_keys command("all_keys") end |
#clear_namespace(namespace:) ⇒ Object
Clear keys in a namespace
110 111 112 |
# File 'lib/mudis_client.rb', line 110 def clear_namespace(namespace:) command("clear_namespace", namespace:) end |
#current_memory_bytes ⇒ Object
Return current memory usage
125 126 127 |
# File 'lib/mudis_client.rb', line 125 def current_memory_bytes command("current_memory_bytes") end |
#delete(key, namespace: nil) ⇒ Object
Delete a value from the Mudis server
80 81 82 |
# File 'lib/mudis_client.rb', line 80 def delete(key, namespace: nil) command("delete", key:, namespace:) end |
#exists?(key, namespace: nil) ⇒ Boolean
Check if a key exists in the Mudis server
85 86 87 |
# File 'lib/mudis_client.rb', line 85 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
90 91 92 93 94 95 96 97 |
# File 'lib/mudis_client.rb', line 90 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 |
#inspect(key, namespace: nil) ⇒ Object
Inspect metadata for a key
100 101 102 |
# File 'lib/mudis_client.rb', line 100 def inspect(key, namespace: nil) command("inspect", key:, namespace:) end |
#keys(namespace:) ⇒ Object
Return keys for a namespace
105 106 107 |
# File 'lib/mudis_client.rb', line 105 def keys(namespace:) command("keys", namespace:) end |
#least_touched(limit = 10) ⇒ Object
Return least touched keys
115 116 117 |
# File 'lib/mudis_client.rb', line 115 def least_touched(limit = 10) command("least_touched", limit:) end |
#max_memory_bytes ⇒ Object
Return max memory configured
130 131 132 |
# File 'lib/mudis_client.rb', line 130 def max_memory_bytes command("max_memory_bytes") end |
#metrics ⇒ Object
Retrieve metrics from the Mudis server
135 136 137 |
# File 'lib/mudis_client.rb', line 135 def metrics command("metrics") end |
#open_connection ⇒ Object
Open a connection to the server (TCP or UNIX)
18 19 20 21 22 23 24 |
# File 'lib/mudis_client.rb', line 18 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
70 71 72 |
# File 'lib/mudis_client.rb', line 70 def read(key, namespace: nil) command("read", key:, namespace:) end |
#request(payload) ⇒ Object
Send a request to the MudisServer and return the response
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mudis_client.rb', line 29 def request(payload) # rubocop:disable Metrics/MethodLength @mutex.synchronize do attempts = 0 begin attempts += 1 response = nil Timeout.timeout(MudisIPCConfig.timeout) do sock = open_connection sock.puts(JSON.dump(payload)) response = sock.gets sock.close end return nil unless response res = JSON.parse(response, symbolize_names: true) raise res[:error] unless res[:ok] res[:value] rescue Errno::ENOENT, Errno::ECONNREFUSED, Timeout::Error if attempts <= MudisIPCConfig.retries retry end 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.}" nil end end end |
#write(key, value, expires_in: nil, namespace: nil) ⇒ Object
Write a value to the Mudis server
75 76 77 |
# File 'lib/mudis_client.rb', line 75 def write(key, value, expires_in: nil, namespace: nil) command("write", key:, value:, ttl: expires_in, namespace:) end |