Skip to content

Instantly share code, notes, and snippets.

@karmi
Created December 7, 2010 10:06
Show Gist options
  • Save karmi/731641 to your computer and use it in GitHub Desktop.
Save karmi/731641 to your computer and use it in GitHub Desktop.
Desigining "trending topics in 24 hours sliding window" with Redis
# ------------------------------------------------------------------
# Desigining "trending topics in 24 hours sliding window" with Redis
# ------------------------------------------------------------------
redis-cli del tophashes:2010-12-07-08-00
redis-cli del tophashes:2010-12-07-09-00
redis-cli del tophashes:current
echo '=== 8:00 AM ==='
echo 'First tweet about "breakfast"'
redis-cli ZINCRBY tophashes:2010-12-07-08-00 1 breakfast
echo 'Second tweet about "breakfast"'
redis-cli ZINCRBY tophashes:2010-12-07-08-00 1 breakfast
echo 'First tweet about "work"'
redis-cli ZINCRBY tophashes:2010-12-07-08-00 1 work
echo '=== 9:00 AM ==='
echo 'Third tweet about "breakfast"'
redis-cli ZINCRBY tophashes:2010-12-07-09-00 1 breakfast
echo 'Second tweet about "work"'
redis-cli ZINCRBY tophashes:2010-12-07-09-00 1 work
echo 'First tweet about "school"'
redis-cli ZINCRBY tophashes:2010-12-07-09-00 1 school
echo
echo 'Trending between 8:00 and 9:00:'
redis-cli ZREVRANGE tophashes:2010-12-07-08-00 0 -1
# 1. "breakfast"
# 2. "work"
echo
echo 'Trending from 9:00:'
redis-cli ZREVRANGE tophashes:2010-12-07-09-00 0 -1
# 1. "work"
# 2. "school"
# 3. "breakfast"
echo
echo 'Currently stored hours:'
redis-cli KEYS tophashes:2010-12-07*
# ---------------------------------------------------------------------------------------------
# Compute union of the last two hours (running periodically, as a background job, etc)
redis-cli ZUNIONSTORE tophashes:current 2 tophashes:2010-12-07-08-00 tophashes:2010-12-07-09-00
# ---------------------------------------------------------------------------------------------
echo
echo 'Trending topics in the last two hours:'
redis-cli ZREVRANGE tophashes:current 0 -1
# 1. "breakfast"
# 2. "work"
# 3. "school"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment