python redis客户端使用lua脚本,redislua,有一个需求,为一个k
python redis客户端使用lua脚本,redislua,有一个需求,为一个k
有一个需求,为一个key设置一个field存储时间戳,每当有新数据,判断新数据时间戳是否>之前的时间戳,如果是,更新时间戳,由于依赖中间执行结果,所以使用lua减少客户端和服务端通信次数
#!/usr/bin/python# -*- coding: utf-8 -*-import redisr = redis.Redis("127.0.0.1")lua = """local key = KEYS[1]local field = ARGV[1]local timestamp_new = ARGV[2]
-- get timestamp of the key in redislocal timestamp_old = redis.call(‘hget‘, key, field)
-- if timestamp_old == nil, it means the key is not existif timestamp_old == nil or timestamp_new > timestamp_old then redis.call(‘hset‘, key, field, timestamp_new)end"""cmd = r.register_script(lua)res = cmd(keys=[‘meterdata_36118_plab_current‘], args=[‘time‘, 1533299183]) # 关键字参数client可以设置执行lua脚本的client instance, 也可以指定其为pipelineprint res
使用pipeline的watch也可以获取pipeline执行期间的结果,但是在获取结果期间还是和服务端进行了通信,增加了网络消耗。
lua脚本也可以放在pipe执行,只需指定client=pipeline
python redis客户端使用lua脚本
相关内容
- python爬微博,python爬,# -*- codi
- 写一个函数,识别字符串是否符合python语法的变量名,
- python3.6执行pip3时 Unable to create process using '&q
- 一个案例深入Python中的__new__和__init__,__new___init_,准备
- python 读取文件第一列 空格隔开的数据,,file=open(
- Mac配置eclipse+pydev+Python遇到的问题,eclipsepydev,最近在研
- python算两个时间之间的天数,将天数转成int型,python
- python——求交通肇事者的车牌号,python交通肇事,例子:
- Python3.X 安装Scrapy,python3.xscrapy,安装Scrapy有两
- Python包和版本管理的最好工具----pipenv,python----pipenv,
评论关闭