1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import redis
import hashlib
r = redis.Redis() # 定义 Lua 脚本
lua_script = """
local key = "daily_data:".. tostring(ARGV[1])
local increment = tonumber(ARGV[2])
local currentValue = redis.call('GET', key)
if currentValue == false then
redis.call('SET', key, increment)
return increment
else
local newValue = tonumber(currentValue) + increment
redis.call('SET', key, newValue)
return newValue
end
""" # 加载脚本并获取 SHA1 校验和
sha = r.script_load(lua_script)
# 使用已加载的脚本执行操作
date = '20241106'
increment_value = 10
new_value = r.evalsha(sha, 2, date, increment_value)
print(f"更新后的单日累加数据:{new_value}")
# 再次使用已加载的脚本执行操作
new_value = r.evalsha(sha, 2, date, increment_value)
print(f"再次更新后的单日累加数据:{new_value}")
|