查找redis中的大key

2016/5/20 posted in  Redis
python find_bigkey.py host 6379 [password]
import sys
import redis
 
 
def find_big_key_normal(db_host, db_port, db_password, db_num):
  client = redis.StrictRedis(host=db_host, port=db_port, password=db_password, db=db_num)
  i=0
  temp = client.scan(cursor=i,count=1000)
  j =0
  while temp[0]>0 :
    i=temp[0]
    j=j+len(temp[1])
    try:
      r = client.pipeline(transaction=False)
      for k in temp[1]:
        r.debug_object(k)
      tempA = r.execute()
      x = 0
      for key in tempA:
        length = key.get("serializedlength")
        ##type = key.get("encoding")
        if length > 10240 :
          type = client.type(temp[1][x])
          print temp[1][x], type,length
        x=x+1
    except :
      print "a execption come"
    temp = client.scan(cursor=i,count=1000)
 
   
if __name__ == '__main__':
  if len(sys.argv) != 4:
     print 'Usage: python ', sys.argv[0], ' host port password '
     exit(1)
  db_host = sys.argv[1]
  db_port = sys.argv[2]
  db_password = sys.argv[3]
  r = redis.StrictRedis(host=db_host, port=int(db_port), password=db_password)
  nodecount = 1
  keyspace_info = r.info("keyspace")
  for db in keyspace_info:
    print 'check ', db, ' ', keyspace_info[db]
    find_big_key_normal(db_host, db_port, db_password, db.replace("db", ""))
删除缓存key: EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 cpx_sn_
EVAL "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,5000 do \n redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) \n end \n return keys" 0 prefix:*