python 连接redis

直接连接:

'''
set(name,value,ex=None,px=None,nx=False,xx = False)
ex 设置过期时间(秒)
px 设置过期时间(毫秒)
nx 如果设置为True , 只有name 不存在时 当前set操作才执行
xx 如果设置为True, 只有name 存在的时候 set操作才执行
'''
import redis
#连接redis server
r = redis.Redis(host='192.168.117.138',port=6379)

#操作
r.set('test','mytest')

print(r.get('test'))

 

 

连接池:

import redis
pool = redis.ConnectionPool(host='192.168.117.138',port=6379)

r = redis.Redis(connection_pool=pool)
r.set('conn','mytest')

print(r.get('conn'))

 

posted on 2017-12-24 21:33  gaizhongfeng  阅读(154)  评论(0编辑  收藏  举报