前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python连接redis

python连接redis

作者头像
py3study
发布2020-01-06 18:08:30
8270
发布2020-01-06 18:08:30
举报
文章被收录于专栏:python3python3

一、安装服务

下载、编译安装

代码语言:javascript
复制
wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar -zxvf redis-3.0.1.tar.gz
cd redis-3.0.1
make

二进制文件是编译完成后在src目录下,通过下面的命令启动Redis服务:

代码语言:javascript
复制
$ src/redis-server &

客户端也可以如下安装:

代码语言:javascript
复制
pip install redis

二、redis连接示例

    redis是以key-value的形式存储的。首先我们将redis所在主机的ip和发布端口作为参数实例化了一个对象r,然后去设置set和取出get值。

例子:

代码语言:javascript
复制
import redis
redis_config = {
"host": "192.168.2.230",
"port": 6379
}
r = redis.Redis(**redis_config)
r.set("name","huangzhenping")
print(r.keys())
print(r.get("name"))

运行结果:

name

huangzhenping

三、连接池

    redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池

例子:

代码语言:javascript
复制
import redis
redis_config = {
    "host": "192.168.2.230",
    "port": 6379
}
pool = redis.ConnectionPool(**redis_config)
r = redis.Redis(connection_pool=pool)
r.set("age","27")
print(r.get("age"))

运行结果:

27

或者将连接池包装成一个函数,方便调用:

代码语言:javascript
复制
import redis
def get_redis_connect():
    redis_config = {
        "host": "192.168.2.230",
        "port": 6379
    }
    
    pool = redis.ConnectionPool(**redis_config)
    r = redis.Redis(connection_pool=pool)
    return r
    
if __name__ == "__main__":
    r = get_redis_connect()
    print(r.keys())

四、管道

    redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。redis服务端会处理完多条命令后会将多条命令的处理结果打包到一起返回给客户端。需要注意到是redis必须在处理完所有命令前先缓存起所有命令的处理结果,打包的命令越多,缓存消耗内存也越多。

例子:对比使用管道和不使用管道处理的时间

代码语言:javascript
复制
import redis
import datetime
def withpipe(r):
    pipe = r.pipeline(transaction=True)
    for i in xrange(1, 1000):
        key = "key1_" + str(i)
        value = "value1_" + str(i)
        pipe.set(key, value)
    pipe.execute()
    
def withoutpipe(r):
    for i in xrange(1, 1000):
        key = "key2_" + str(i)
        value = "value2_" + str(i)
        r.set(key, value)
        
redis_config = {
    "host": "192.168.2.230",
    "port": 6379,
    "db": 0
}

if __name__ == "__main__":
    pool = redis.ConnectionPool(**redis_config)
    r1 = redis.Redis(connection_pool=pool)
    r2 = redis.Redis(connection_pool=pool)
    start = datetime.datetime.now()
    
    withpipe(r1)
    end = datetime.datetime.now()
    t_time = (end - start).microseconds
    print("withpipe time is: {0}".format(t_time))
    start = datetime.datetime.now()
    
    withoutpipe(r2)
    end = datetime.datetime.now()
    t_time = (end - start).microseconds
    print("withoutpipe time is: {0}".format(t_time))

运行结果:

withpipe time is: 17000

withoutpipe time is: 105000

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档