M
M
mitaichik2017-01-23 14:14:16
Redis
mitaichik, 2017-01-23 14:14:16

How to count the number of events per hour?

Hello!
The task is this: to count the number of events per hour in order to limit the number of calls to the API for a specific user.
I thought to shove a key into redis something like userId_eventName_time and set it to expire in one hour, and then get the number by getting the number according to the userId_eventName_* template. But this approach implies the creation of a huge number of keys and is hardly good.
Is there any other way to account for the number of events per hour?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Dart, 2017-01-23
@gobananas

If the ultimate goal is to limit the amount, then it seems to me more economical in terms of resources to shift this work not to the application, but to the server, for example, using iptables It is done something
like this wbtools.ru/?forum=22
can find.

V
Victor Diditsky, 2017-01-24
@GhostSt92

The following solution is possible:
Create a table user_log (id, user_id, date) and add an entry to this table each time it is accessed.
Next, you pull the number of hits for a certain period of time:

SELECT COUNT(*) 
FROM `user_log`
WHERE `date` >= '2016-01-24 18:00:00'
AND `date` < '2016-01-24 17:00:00'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question