redis字典取数据、列表取数据(数据量大)
一、如果redis得k4 对应得字典中有1000w条数据,请打印所有得数据
hscan_iter("k4",count=100) # 一次取100条,取完了之后,再去取100条;
# -*- coding:utf-8 -*- import redis pool = redis.ConnectionPool(host="10.0.0.200", port=6379, password="luffy1234", max_connections=1000) conn = redis.Redis(connection_pool=pool) # 字典操作 """ redis = { k4:{ username:alex age:18 } } """ print(conn.hlen('k4')) ret = conn.hscan_iter("k4",count=1) for item in ret: print(item)
二、如果redis得列表有1000w条数据,请打印所有得数据
没有scan_iter需要自己去实现,利用生成器
# 生成器给列表 。。一点一点 设置 缓存取数据; # conn.lpush("k3",*[1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20]) # print(conn.lrange("k3",0,100)) def list_iter(key,count=100): index = 0 while True: data_list = conn.lrange(key,index,index+count-1) if not data_list: return index += count for item in data_list: yield item res = list_iter("k3",count=10) for item in res: print(item)