Class: MemCache

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/memcache.rb

Instance Method Summary collapse

Instance Method Details

#all_keysObject

Raises:

  • (MemCacheError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sinatra/memcache.rb', line 5

def all_keys
  raise MemCacheError, "No active servers" unless active?
  keys = []

  @servers.each do |server|
    sock = server.socket
    raise MemCacheError, "No connection to server" if sock.nil?

    begin
      sock.write "stats items\r\n"
      slabs = {}
      while line = sock.gets
        break if line == "END\r\n"
        slabs[$1] = $2 if line =~ /^STAT items:(\d+):number (\d+)/ 
      end

      slabs.each do |k, v|
        sock.write "stats cachedump #{k} #{v}\r\n"
        while line = sock.gets
          break if line == "END\r\n"
          prefix = @namespace.empty? ? '' : "#{@namespace}:"
          r = Regexp.new("^ITEM #{prefix}([^\s]+)")
          keys << $1 if line =~ r
        end
      end
    rescue SocketError, SystemCallError, IOError => err
      server.close
      raise MemCacheError, err.message
    end
  end

  keys
end