Class: Rubcask::Server::Client
- Inherits:
- 
      Object
      
        - Object
- Rubcask::Server::Client
 
- 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
- 
  
    
      .with_client(host, port) {|the| ... } ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    yields a new client to the block closes the client after the block is terminated. 
Instance Method Summary collapse
- 
  
    
      #close  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Close the client. 
- 
  
    
      #del(key)  ⇒ Protocol::OK, Protocol::NIL 
    
    
  
  
  
  
  
  
  
  
  
    Remove value associated with the key. 
- 
  
    
      #get(key)  ⇒ String, Protocol::NIL 
    
    
  
  
  
  
  
  
  
  
  
    Get value associated with the key. 
- 
  
    
      #initialize(host, port)  ⇒ Client 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Client. 
- 
  
    
      #ping  ⇒ Protocol::PONG 
    
    
  
  
  
  
  
  
  
  
  
    Ping the server Use this method to check if server is running and responding. 
- 
  
    
      #pipelined(&block)  ⇒ Array<String> 
    
    
  
  
  
  
  
  
  
  
  
    Run the block in the pipeline. 
- 
  
    
      #set(key, value)  ⇒ Protocol::OK, Protocol::ERROR 
    
    
  
  
  
  
  
  
  
  
  
    Set value associated with the key. 
- 
  
    
      #setex(key, value, ttl)  ⇒ String, Protocol::NIL 
    
    
  
  
  
  
  
  
  
  
  
    Ping the server Use this method to check if server is running and responding. 
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.
| 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
| 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
#close ⇒ Object
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
| 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
| 43 44 45 | # File 'lib/rubcask/server/client.rb', line 43 def get(key) call_method("get", key) end | 
#ping ⇒ Protocol::PONG
Ping the server Use this method to check if server is running and responding
| 70 71 72 | # File 'lib/rubcask/server/client.rb', line 70 def ping call_method("ping") end | 
#pipelined(&block) ⇒ Array<String>
pipeline execution IS NOT atomic
instance_eval is used so you can call methods directly instead of using block argument
Run the block in the pipeline
| 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
| 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
| 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 |