redis:每天重置计数器 [英] redis: reset counter every day
问题描述
我希望每天使用Redis重置计数器.我是Redis的新手,所以我想确保我充分了解事务和管道的工作方式.
I am looking to reset a counter every day using Redis. I am new to Redis so I want to make sure I well understood how transactions and pipes work.
以下代码是否可以确保在多进程环境中工作时,我总是得到一对唯一的(日期,数字),还是我需要使用Redis锁?
Does the following code ensure that I will always get a unique couple of (date, number) while working in a multi processes environment or do I need to use a Redis lock?
import datetime
import redis
r = redis.Redis(...)
def get_utc_date_now():
return datetime.datetime.utcnow().date()
def get_daily_counter(r, dt_key='dt', counter_key='counter'):
def incr_daily_number(pipe):
dt_now = get_utc_date_now().isoformat() # e.g.: "2014-10-18"
dt = pipe.get(dt_key)
pipe.multi()
if dt != dt_now:
pipe.set(dt_key, dt_now)
pipe.set(counter_key, 0)
pipe.get(dt_key)
pipe.incr(counter_key)
result = r.transaction(incr_daily_number, dt_key)
return result[-2:]
# Get the (dt, number) couple
# 2014-10-18, 1
# 2014-10-18, 2
# etc.
dt, number = get_daily_counter(r)
更新
尝试使用LUA脚本:
UPDATE
Try with LUA Script:
r = redis.Redis(...)
incr_with_reset_on_change_lua_script = """
local dt = redis.call('GET', KEYS[2])
if dt ~= ARGV[2] then
redis.call('MSET', KEYS[1], ARGV[1], KEYS[2], ARGV[2])
end
return redis.call('INCR', KEYS[1])
"""
# Incr KEYS1 but reset first if KEYS2 has changed.
incr_with_reset_on_change = r.register_script(incr_with_reset_on_change_lua_script)
counter_key = 'dcounterA'
watch_key = 'dcounterA_dt'
watch_value = get_utc_date_now().isoformat()
number = incr_with_reset_on_change(keys=[counter_key, watch_key], args=[reset_value, watch_value])
推荐答案
考虑在午夜发生的两个并发事务.两者都可以执行get(dt_key),但是其中一个将首先执行MULTI/EXEC块.它将重置计数器,设置新日期,增加计数器.第二个也将输入其MULTI/EXEC块中,但是由于'dt'的值已更改,执行将失败,并且将再次调用incr_daily_number.这次get(dt_key)将返回新日期,因此当执行MULTI/EXEC块时,计数器将递增而不进行任何复位.两项交易将返回具有不同计数器值的新日期.
Consider two concurrent transactions occuring at midnight. Both can execute get(dt_key), but one will execute the MULTI/EXEC block first. It will reset the counter, set the new date, increment the counter. The second one will enter also in its MULTI/EXEC block, but because the value of 'dt' has changed, the execution will fail, and incr_daily_number will be called again. This time get(dt_key) will return the new date, so when the MULTI/EXEC block will be executed, the counter will be incremented without any reset. The two transactions will return the new date with different counter values.
所以,我相信这里没有比赛条件,并且(日期,数字)对将是唯一的.
So, I believe there is no race condition here, and that the (date,number) couples will be unique.
您还可以使用服务器端Lua脚本(其执行始终是原子的)来实现此目的.通常比较方便.
You could also have implemented this using a server-side Lua script (whose execution is always atomic). It is usually more convenient.
请注意,实际上,没有Redis锁之类的东西. API中可用的锁定机制由Python客户端提供,而不是由Redis服务器提供.如果您看一下它的实现,您将会意识到它也是基于SETNX + WATCH/MULTI/EXEC块或Lua脚本编写的.
Note that actually, there is no such thing as a Redis lock. The locking mechanism available in the API is provided by the Python client - not by the Redis server. If you look at its implementation, you will realize it is also based on SETNX + WATCH/MULTI/EXEC blocks or Lua scripting.
这篇关于redis:每天重置计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!