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