Class: RedisScanner::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_scanner/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Redis

Returns a new instance of Redis.



7
8
9
10
# File 'lib/redis_scanner/redis.rb', line 7

def initialize(options)
  @options = options
  @client = ::Redis.new extract_redis_options(options)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/redis_scanner/redis.rb', line 5

def client
  @client
end

Instance Method Details

#get_type_and_size(key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redis_scanner/redis.rb', line 27

def get_type_and_size(key)
  type = @client.type key
  size = case type
    when "string"
      @client.strlen key
    when "list"
      @client.llen key
    when "hash"
      @client.hlen key
    when "set"
      @client.scard key
    when "zset"
      @client.zcard key
    else
      1
  end
  [type, size.to_i]
end

#scan(*args) ⇒ Object



12
13
14
# File 'lib/redis_scanner/redis.rb', line 12

def scan(*args)
  @client.scan *args
end

#total_keysObject

total keys for given db(default 0)



17
18
19
20
21
22
23
24
25
# File 'lib/redis_scanner/redis.rb', line 17

def total_keys
  ret = 0
  if (info = @client.info) && (str = info["db#{@options[:db].to_i}"])
    if m = str.scan(/keys=(\d+)/)
      ret = m.flatten.first.to_i
    end
  end
  ret
end