Python Redis client uses LUA scripts

Source: Internet
Author: User

There is a need to set a field for a key to store timestamps, whenever there is new data, to determine whether the new data timestamp is > before the timestamp, if so, update the timestamp, due to rely on intermediate execution results, so use LUA to reduce the number of client and server-side communication
#!/usr/bin/python#-*-coding:utf-8-*-ImportREDISR= 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]) # keyword parameter The client can set the client instance to execute the Lua script, or it can be specified as pipelinePrintRes

Using pipeline Watch also gets the results of the pipeline execution period, but it also communicates with the server during the acquisition of the results, increasing network consumption.

Lua scripts can also be placed in pipe execution, just specify Client=pipeline

Python Redis client uses LUA scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.