Class: RubyMemcached::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-memcached/Server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



13
14
15
16
17
18
# File 'lib/ruby-memcached/Server.rb', line 13

def initialize(host, port)
    @host = host
    @port = port
    @connection = TCPServer.new(host, port)
    @memc = Memcached.new()
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/ruby-memcached/Server.rb', line 9

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/ruby-memcached/Server.rb', line 10

def port
  @port
end

#storageObject

Returns the value of attribute storage.



11
12
13
# File 'lib/ruby-memcached/Server.rb', line 11

def storage
  @storage
end

Instance Method Details

#add(client, command) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby-memcached/Server.rb', line 119

def add(client, command)
    key = command['key']
    flags = command['flags']
    exptime = command['exptime']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    noreply = !command['noreply'].nil?
    
    response = @memc.add(key, flags, exptime, bytes, data)
    client.puts(response) unless noreply
end

#append(client, command) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/ruby-memcached/Server.rb', line 143

def append(client, command)
    key = command['key']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    noreply = !command['noreply'].nil?
    
    response = @memc.append(key, bytes, data)
    client.puts(response) unless noreply
end

#cas(client, command) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ruby-memcached/Server.rb', line 164

def cas(client, command)
    key = command['key']
    flags = command['flags']
    exptime = command['exptime']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    cas_id = command['cas_id'].to_i()
    noreply = !command['noreply'].nil?
    
    response = @memc.cas(key, flags, exptime, bytes, cas_id, data)
    client.puts(response) unless noreply
end

#delete(client, command) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/ruby-memcached/Server.rb', line 177

def delete(client, command)
    key = command['key']
    noreply = !command['noreply'].nil?

    response = @memc.delete(key)
    client.puts(response) unless noreply
end

#get(client, command) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ruby-memcached/Server.rb', line 87

def get(client, command)
    keys = command['keys'].split(' ')
    items = @memc.get_multi(keys)
    
    for item in items
        client.puts(Responses.get % [item.key, item.flags, item.bytes, item.data]) unless item.nil?()
    end
    client.puts(Responses.end)
end

#get_data(client, bytes) ⇒ Object



83
84
85
# File 'lib/ruby-memcached/Server.rb', line 83

def get_data(client, bytes)
    return client.read(bytes + 1).chomp()
end

#gets(client, command) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/ruby-memcached/Server.rb', line 97

def gets(client, command)
    keys = command['keys'].split(' ')
    items = @memc.get_multi(keys)
    
    for item in items
        client.puts(Responses.gets % [item.key, item.flags, item.bytes, item.cas_id, item.data]) unless item.nil?()
    end
    client.puts(Responses.end)
end

#parse(command, client) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby-memcached/Server.rb', line 45

def parse(command, client)
    case command
        when CommandsRegex.get
            self.get(client, $~)
    
        when CommandsRegex.gets
            self.gets(client, $~)
    
        when CommandsRegex.set
            self.set(client, $~)
    
        when CommandsRegex.add
            self.add(client, $~)
            
        when CommandsRegex.replace
            self.replace(client, $~)
    
        when CommandsRegex.append
            self.append(client, $~)
    
        when CommandsRegex.prepend
            self.prepend(client, $~)
    
        when CommandsRegex.cas
            self.cas(client, $~)

        when CommandsRegex.delete
            self.delete(client, $~)

        when CommandsRegex.end
            return false;
        else
            client.puts(Errors.client_error % [": Invalid command"])
    end
    
    return true
end

#prepend(client, command) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/ruby-memcached/Server.rb', line 153

def prepend(client, command)
    key = command['key']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    noreply = !command['noreply'].nil?
    
    response = @memc.prepend(key, bytes, data)
    client.puts(response) unless noreply
    
end

#replace(client, command) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ruby-memcached/Server.rb', line 131

def replace(client, command)
    key = command['key']
    flags = command['flags']
    exptime = command['exptime']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    noreply = !command['noreply'].nil?
    
    response = @memc.replace(key, flags, exptime, bytes, data)
    client.puts(response) unless noreply
end

#set(client, command) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby-memcached/Server.rb', line 107

def set(client, command)
    key = command['key']
    flags = command['flags']
    exptime = command['exptime']
    bytes = command['bytes'].to_i()
    data = self.get_data(client, bytes)
    noreply = !command['noreply'].nil?
    
    response = @memc.set(key, flags, exptime, bytes, data)
    client.puts(response) unless noreply
end

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby-memcached/Server.rb', line 20

def start
    loop do
        Thread.start(@connection.accept()) do | client |
        
            puts('Opening connection to %s' % client.to_s)
    
            continue_condition = true
    
            while command = client.gets()
                printf('%s: %s' % [client.to_s(), command])
                begin
                    continue_condition = parse(command, client) unless command.nil?                        
                rescue => e
                    client.puts(Errors.server_error % e.message)
                    puts(Errors.server_error % e.message)
                end
                break unless continue_condition
            end
    
            client.close()
            puts('Closing connection to %s' % [client.to_s()])
        end
    end
end

#start_gc(interval, loops = nil) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/ruby-memcached/Server.rb', line 185

def start_gc(interval, loops = nil)

    while loops.nil?() || loops > 0
        sleep(interval)
        puts('Garbage collector deleted: %d' % @memc.check_exptimes())
        loops -= 1 unless loops.nil?()
    end
end