pip3 install redis
import redis
#创建redis链接对象
r = redis.Redis(host='192.168.196.131',port=6379,decode_responses=True,password='newpwd')
#存储键值对
r.set('site','www.qi.cn')
#获取值
print(r.get('site'))
#指定decode_responses为True,表示输出为字符串
red = redis.StrictRedis(host='192.168.196.131',port=6379,decode_responses=True,password='newpwd')
#默认redis入库编码是utf-8,如果要修改的话,需要指明 charset 和 decode_responsers 为True
#test = redis.StrictRedis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)
red.lpush('list1','mongdb','redis','mysql')
print(r.lrange('list1',0,-1))
print(r.llen('list1'))
# #output:
# www.qi.cn
# ['mysql', 'redis', 'mongdb']
import redis
class redisHelper():
def __init__(self):
# 创建redis链接对象
self.redis = redis.Redis(host='192.168.196.131', port=6379, decode_responses=True, password='newpwd',db=0)
def redis_set(self,name,value):
self.redis.set(name, value)
def redis_get(self,name):
return self.redis.get(name)
def main():
rh = redisHelper()
rh.redis_set('site','www.baidu.com')
print(rh.redis_get('site'))
if __name__ == '__main__':
main()
import redis
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logfile = './redis-key.log'
fh = logging.FileHandler(logfile, mode='a')
fh.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
class redisHelper():
def __init__(self):
# 创建redis链接对象
self.redis = redis.Redis(host='192.168.196.131', port=6379, decode_responses=True, password='newpwd',db=0)
def redis_set(self,name,value):
self.redis.set(name, value)
def redis_get(self,name):
return self.redis.get(name)
def redis_info(self):
return self.redis.info()
def redis_info_total_keys(self):
return self.redis.info()['db0']['keys']
def main():
rh = redisHelper()
# for i in range(0,1000000):
for i in range(653012,2000000):
rh.redis_set('key' + str(i), i)
logging.info("keys Total = %s" % rh.redis_info_total_keys())
if __name__ == '__main__':
main()