Created
May 16, 2011 11:47
-
-
Save karmi/974306 to your computer and use it in GitHub Desktop.
Counters with Redis Sorted Sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# =============================== | |
# Counters with Redis Sorted Sets | |
# =============================== | |
redis-cli DEL downloads:total downloads:total:today downloads:files:total downloads:files:2011-05-10 | |
# -------------- | |
# STORING COUNTS | |
# -------------- | |
# On each hit for file, do this: | |
# > Totals | |
redis-cli INCR downloads:total | |
redis-cli INCR downloads:total:today | |
# > Individual files | |
# GET /downloads/file1.mpg | |
redis-cli ZINCRBY downloads:files:total 1 /downloads/file1.mpg | |
redis-cli ZINCRBY downloads:files:2011-05-10 1 /downloads/file1.mpg | |
# GET /downloads/file1.mpg | |
redis-cli ZINCRBY downloads:files:total 1 /downloads/file1.mpg | |
redis-cli ZINCRBY downloads:files:2011-05-10 1 /downloads/file1.mpg | |
# GET /downloads/file2.mpg | |
redis-cli ZINCRBY downloads:files:total 1 /downloads/file2.mpg | |
redis-cli ZINCRBY downloads:files:2011-05-10 1 /downloads/file2.mpg | |
# > Set expire at 2011-05-10 23:59:59 | |
redis-cli EXPIREAT downloads:total:today 1305064799 | |
# -------------- | |
# GETTING COUNTS | |
# -------------- | |
# First, the obvious. | |
echo '> Total counts, all time' | |
redis-cli GET downloads:total | |
echo '> Total counts, today' | |
redis-cli GET downloads:total:today | |
echo '> 10 most downloaded files, all the time' | |
redis-cli ZREVRANGE downloads:files:total 0 10 WITHSCORES | |
echo '> 10 most downloaded files on 2011-05-10' | |
redis-cli ZREVRANGE downloads:files:2011-05-10 0 10 WITHSCORES | |
# Second, timelines. | |
echo '> 10 most downloaded files between 2011-05-10 and <OTHER DATE>' | |
redis-cli ZUNIONSTORE downloads:timeline 2 downloads:files:2011-05-10 downloads:files:2011-05-09 # <OTHER DATE> | |
redis-cli ZREVRANGE downloads:timeline 0 10 WITHSCORES |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment