python redis 关于pipline(),lua脚本和scan命令的初步使用

  • conn=redis.StrictRedis(connection_pool=config_manager_list[str(app_id)].redis_pool,socket_timeout=5,socket_connect_timeout=5, retry_on_timeout=5) 连接数据库

  • r = conn.pipeline() 获取管道

使用pipelining 发送命令时,redis server必须部分请求放到队列中(使用内存)执行完毕后一次性发送结果
pipeline期间将“独占”链接,此期间将不能进行非“管道”类型的其他操作,直到pipeline关闭;如果你的pipeline的指令集很庞大,为了不干扰链接中的其他操作,你可以为pipeline操作新建Client链接,让pipeline和其他正常操作分离在2个client中

  • r.hset(name, "JWT", new_JWT) 更新key为name的哈希记录里叫做 JWT字段的值为new_JWT
  • r.rename(old_name, new_name) 将old_name对应的哈希记录的key更新为new_name
  • r.execute() 执行命令

pipeline和 execute合起来相当于使用事务来操作,可以一次性执行多个命令

#!/usr/bin/python2
import redis
import time
def without_pipeline():
    r=redis.Redis()
    for i in range(10000):
        r.ping()
    return
def with_pipeline():
    r=redis.Redis()
    pipeline=r.pipeline()
    for i in range(10000):
        pipeline.ping()
    pipeline.execute()
    return
def bench(desc):
    start=time.clock()
    desc()
    stop=time.clock()
    diff=stop-start
    print "%s has token %s" % (desc.func_name,str(diff))
if __name__=='__main__':
    bench(without_pipeline)
    bench(with_pipeline)

# 测试结果
without_pipeline has token 4.64443057954
with_pipeline has token 0.098383873589

python 调用lua脚本操作redis

  • s = self.conn.register_script(GET_COUNT_SCRIPT)
  • res = s(args=patterns)
GET_ALL_RID_SCRIPT = """
local cursor = "0"
local matchKey = ARGV[1]
local matchKeyPrefix = string.sub(matchKey,1,-2)
local ridList = {};
local done = false;
repeat
    local result = redis.call("SCAN", cursor, "match", matchKey)
    cursor = result[1];
    for i, key in ipairs(result[2]) do
        local rid = string.gsub(key,matchKeyPrefix,"");
        table.insert(ridList,rid);
    end
    if cursor == "0" then
        done = true;
    end
until done

return ridList;
"""

s = self.conn.register_script(GET_ALL_RID_SCRIPT)
res = s(args=[match])

参考链接

scan 命令

通过key '*' 查找所有key
无匹配模式遍历
有匹配模式遍历
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictR...
    君惜丶阅读 232,125评论 15 117
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,810评论 19 139
  • 1.1 资料 ,最好的入门小册子,可以先于一切文档之前看,免费。 作者Antirez的博客,Antirez维护的R...
    JefferyLcm阅读 17,201评论 1 51
  • 安全性 设置客户端连接后进行任何其他指令前需要使用的密码。 警告:因为redis 速度相当快,所以在一台比较好的服...
    OzanShareing阅读 5,837评论 1 7
  • 笙歌一曲醉寒泪, 捂泣挥影笑离殇; 梦与佳人共一舞, 却是天涯各一方。
    雾夜忧魂阅读 3,529评论 3 20

友情链接更多精彩内容