安装redis模块: pip install redis 普通连接:
import redis
conn = redis.Redis(host='127.0.0.1', port=6379)
# 可以使用url方式连接到数据库
# conn = Redis.from_url('redis://@localhost:6379/1')
conn.set('name', 'LinWOW')
print(conn.get('name'))
使用连接池:
from redis import ConnectionPool
POOL=ConnectionPool(host='127.0.0.1',port=6379,max_connections=100)
conn = redis.Redis(connection_pool=POOl)
conn.set('name', 'LinWOW')
print(conn.get('name'))
参考:https://blog.csdn.net/linwow/article/details/95227392