Class: Rubcask::Server::Client

Inherits:
Object
  • Object
show all
Includes:
Protocol
Defined in:
lib/rubcask/server/client.rb

Defined Under Namespace

Classes: InvalidResponseError

Constant Summary

Constants included from Protocol

Protocol::ERROR, Protocol::NIL, Protocol::OK, Protocol::PING, Protocol::PONG, Protocol::SEPARATOR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

create_call_message, encode_message, error_message, nil_message, ok_message, ping_message, pong_message

Constructor Details

#initialize(host, port) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String)

    hostname of the server

  • port (String)

    port of the server



34
35
36
# File 'lib/rubcask/server/client.rb', line 34

def initialize(host, port)
  @socket = TCPSocket.new(host, port)
end

Class Method Details

.with_client(host, port) {|the| ... } ⇒ Object

yields a new client to the block closes the client after the block is terminated

Parameters:

  • host (String)

    hostname of the server

  • port (String)

    port of the server

Yield Parameters:

  • the (Client)

    running client



23
24
25
26
27
28
29
30
# File 'lib/rubcask/server/client.rb', line 23

def self.with_client(host, port)
  client = new(host, port)
  begin
    yield client
  ensure
    client.close
  end
end

Instance Method Details

#closeObject

Close the client



100
101
102
# File 'lib/rubcask/server/client.rb', line 100

def close
  @socket.close
end

#del(key) ⇒ Protocol::OK, Protocol::NIL

Remove value associated with the key

Parameters:

  • key (String)

Returns:

Raises:



62
63
64
# File 'lib/rubcask/server/client.rb', line 62

def del(key)
  call_method("del", key)
end

#get(key) ⇒ String, Protocol::NIL

Get value associated with the key

Parameters:

  • key (String)

Returns:

  • (String)

    Binary string representing the value

  • (Protocol::NIL)

    If no data associated with the key

Raises:



43
44
45
# File 'lib/rubcask/server/client.rb', line 43

def get(key)
  call_method("get", key)
end

#pingProtocol::PONG

Ping the server Use this method to check if server is running and responding

Returns:

Raises:



70
71
72
# File 'lib/rubcask/server/client.rb', line 70

def ping
  call_method("ping")
end

#pipelined(&block) ⇒ Array<String>

Note:

pipeline execution IS NOT atomic

Note:

instance_eval is used so you can call methods directly instead of using block argument

Run the block in the pipeline

Returns:

  • (Array<String>)

    List of responses to the executed methods

Raises:



92
93
94
95
96
97
# File 'lib/rubcask/server/client.rb', line 92

def pipelined(&block)
  pipeline = Pipeline.new
  pipeline.instance_eval(&block)
  call(pipeline.out)
  pipeline.count.times.map { get_response }
end

#set(key, value) ⇒ Protocol::OK, Protocol::ERROR

Set value associated with the key

Parameters:

  • key (String)
  • value (String)

Returns:

Raises:



53
54
55
# File 'lib/rubcask/server/client.rb', line 53

def set(key, value)
  call_method("set", key, value)
end

#setex(key, value, ttl) ⇒ String, Protocol::NIL

Ping the server Use this method to check if server is running and responding

Parameters:

  • key (String)
  • value (String)
  • ttl (Integer, String)

Returns:

  • (String)

    Binary string representing the value

  • (Protocol::NIL)

    If no data associated with the key

Raises:



82
83
84
# File 'lib/rubcask/server/client.rb', line 82

def setex(key, value, ttl)
  call_method("setex", key, value, ttl.to_s)
end