Reputation: 78342
I am using python to increment a counter in 5 minute intervals in redis and speed of the execution code is rather important. I will have a sorted set that will look like this:
1:30 incr 1:35 incr ...
where the incr is the hit count between that 5 minute interval.
Lets say I am at 1:32 pm and I want to place all of the hits between 1:30 and 1:35 in that bucket.
Naively I can just loop through a list and check if minute between 30 and 35 and place in the 1:30 bucket. Is there a more pythonic method to do this?
Upvotes: 3
Views: 1689
Reputation: 34014
from redis import Redis
r = Redis(db=1)
r.flushdb()
def store(t):
h, m = map(int, t.split(':'))
m = m - m % 5
r.zincrby('tc', '%d:%02d' % (h, m), 1)
store('1:02')
store('1:30')
store('1:32')
store('1:35')
print r.zrange('tc', 0, -1, withscores=True)
Output:
[('1:00', 1.0), ('1:35', 1.0), ('1:30', 2.0)]
Upvotes: 4