python - Keeping Track of a Daily Quota (Auto-Expiring Increment) - Redis or Pymongo? -


The basic problem is very simple: there is an ongoing list of entries every time something happens (we say the file is downloaded ) - Idea will be to run the total number of times it will download, but after that the total expiration of the total expiration of the total 24 hours will not be completed (hence total zero should not be reset, but rather than 24 hours earlier downloads Get out of it Should).

(If increment action redis , someone can please me now)

The strange situation is that one There is a small application that uses redis to keep track of repeatedly updated values, and mangodibi pimongo for long term storage With . I am about to apply a daily quota on some functions, which can be tracked in either Redis or MongoDB

Radis one The problem is that it is basically flat, so for every download to expire (compared to the total), someone has to make different items: i.e.

  Cache.set ('filename1.downloads.action_234612', {'download': 1) cache.quarter ('filename1.downloads.action_234612', 86400)  

find out again Sector number of downloads has went up to a daily limit, I can add the object count went cache identified by 'user1.votes.action_ *'.

As a second option, I see that in a Pimango collection with timestamps, it will be for throwing votes - and then ignore items, where time.time () - download_timestamp> ; 86400 (not just SQL because mongodi is already being used).

I know that there is probably a faster way to do this at a lower level - but I'm not sure it would be appropriate to finish the performance difference with it (at least it seems that the item set is indexed To do this without doing it, and only to work on the top items of a stack)

Note that the server goes down or, otherwise, is a integrity check that initially runs and the total number of times the file is downloaded (or whatever is growing) is tracking.

I can not tell you whether you want to use MongoDB or Redis, but how is it I will do it with radis.

For every counter, place the downloaded file, a sorted set. Members of the sorted set should represent download actions and their score is a timestamp of action. Sorted set trim sets the end time for updating the old item and for the whole set.

To add a download to the counter the workflow is something like that in pseudo-Python (note that redis -py has changed the order of the member and the score):

< Pre> r.zadd ('filename1: download', 'action_234612', time.time ()) r.zremrangebyscore ('filename1: downloads', '-inf', time.time () - 86400) r. Expiry ('filename1: downloads', 86400)

You probably want to use a multi / EXEC block above or, alternatively, use the server side lua script for it.

Now that it is hard work, getting the current download count for the file (i.e. during the last 24 hours) is a simple matter (you can also trim it here): < / P>

  downloads = r.zcount ('filename1: downloads', time.time () - 86400, time.time ())  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -