Class: MemcachedServer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/memcached-server/client.rb

Overview

Class that communicates with a MemcachedServer::Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, port) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
# File 'lib/memcached-server/client.rb', line 19

def initialize(hostname, port)

    @hostname = hostname
    @port = port
    @server = TCPSocket.new(hostname, port)

end

Instance Attribute Details

#hostnameString, ipaddress (readonly)

The client socket hostname or IP address

Returns:

  • (String, ipaddress)


12
13
14
# File 'lib/memcached-server/client.rb', line 12

def hostname
  @hostname
end

#portport (readonly)

The client socket port

Returns:



17
18
19
# File 'lib/memcached-server/client.rb', line 17

def port
  @port
end

Instance Method Details

#add(key, flags, exptime, bytes, data_block) ⇒ String

Sends the server an add command

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



50
51
52
53
54
55
# File 'lib/memcached-server/client.rb', line 50

def add(key, flags, exptime, bytes, data_block)
  command = "add #{key} #{flags} #{exptime} #{bytes}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end

#append(key, bytes, data_block) ⇒ String

Sends the server an append command

Parameters:

  • key (String)

    The key of the item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



79
80
81
82
83
84
# File 'lib/memcached-server/client.rb', line 79

def append(key, bytes, data_block)
  command = "append #{key} #{bytes}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end

#cas(key, flags, exptime, bytes, cas_id, data_block) ⇒ String

Sends the server a cas command

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • cas_id (Integer)

    Is a unique integer value

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



108
109
110
111
112
113
# File 'lib/memcached-server/client.rb', line 108

def cas(key, flags, exptime, bytes, cas_id, data_block)
  command = "cas #{key} #{flags} #{exptime} #{bytes} #{cas_id}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end

#endString

Sends the server an end command

Returns:

  • (String)

    The reply that describes the result of the operation



203
204
205
206
# File 'lib/memcached-server/client.rb', line 203

def end()
  @server.puts('END')
  return @server.gets()
end

#get(keys) ⇒ [MemcachedServer::Item]

Sends the server a get command

Parameters:

  • keys ([String])

    The keys of the items to retrieve

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/memcached-server/client.rb', line 119

def get(keys)
  @server.puts("get #{keys}")

  n = keys.split(' ').length()
  retrieved = {}

  n.times do
    loop do

      case @server.gets()

      when ReplyFormat::GET

        key = $~[:key]
        flags = $~[:flags].to_i()
        bytes = $~[:bytes].to_i()
        data_block = @server.read(bytes + 1).chomp()

        item = Item.new(key, flags, 0, bytes, data_block)
        retrieved[key.to_sym] = item

      when ReplyFormat::END_

        break

      else
        
        puts "Error\nServer: #{$_}"
        break

      end

    end

  end

  return retrieved
end

#gets(keys) ⇒ [MemcachedServer::Item]

Sends the server a gets command

Parameters:

  • keys ([String])

    The keys of the items to retrieve

Returns:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/memcached-server/client.rb', line 162

def gets(keys)
  @server.puts("gets #{keys}")

  n = keys.split(' ').length()
  retrieved = {}

  n.times do

    loop do

      case @server.gets()
      when ReplyFormat::GETS
        key = $~[:key]
        flags = $~[:flags].to_i()
        bytes = $~[:bytes].to_i()
        cas_id = $~[:cas_id].to_i()
        data_block = @server.read(bytes + 1).chomp()

        item = Item.new(key, flags, 0, bytes, data_block)
        item.cas_id = cas_id
        retrieved[key.to_sym] = item

      when ReplyFormat::END_
        break

      else
        puts "Error\nServer: #{$_}"
        break

      end

    end

  end

  return retrieved
end

#prepend(key, bytes, data_block) ⇒ String

Sends the server a prepend command

Parameters:

  • key (String)

    The key of the item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



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

def prepend(key, bytes, data_block)
  command = "prepend #{key} #{bytes}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end

#replace(key, flags, exptime, bytes, data_block) ⇒ String

Sends the server a replace command

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



66
67
68
69
70
71
# File 'lib/memcached-server/client.rb', line 66

def replace(key, flags, exptime, bytes, data_block)
  command = "replace #{key} #{flags} #{exptime} #{bytes}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end

#set(key, flags, exptime, bytes, data_block) ⇒ String

Sends the server a set command

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



35
36
37
38
39
40
# File 'lib/memcached-server/client.rb', line 35

def set(key, flags, exptime, bytes, data_block)
  command = "set #{key} #{flags} #{exptime} #{bytes}\n#{data_block}\n"
  @server.puts(command)

  return @server.gets()
end