josiahcarlson / redis-in-action

Example code from the book

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

questions about errata for listing 2.10

scrcls opened this issue · comments

There is a bug on the third line of the rescale_viewed() function definition.

The full function definition reads:

def rescale_viewed(conn):
    while not QUIT:
        conn.zremrangebyrank('viewed:', 20000, -1)
        conn.zinterstore('viewed:', {'viewed:': .5})
        time.sleep(300)
The third line, updated inline, should read:

def rescale_viewed(conn):
    while not QUIT:
        conn.zremrangebyrank('viewed:', 0, -20001)
        conn.zinterstore('viewed:', {'viewed:': .5})
        time.sleep(300)

the old version code remove the items not in the top 20000,
the new version code keep the items in the last 20000.

I can not understand why you change the code, can you tell me, thanks

ZREMRANGEBYRANK <key> <start> <end> deletes items from a ZSET at <key> with indexes from <start> to <end>, inclusive. An item with a lower score will have a lower rank.

The original code deleted the 20k most popular items, rather than leaving the 20k most popular items. 0, -20001 are the right arguments. If you have more questions, you can keep commenting on this bug, but I'm closing it because the errata and updated code in the repository are correct.

the lowest item in view: is the most popular item.0, -20001 will delete the most popular item.

You are incorrect.

127.0.0.1:6379> zadd scores 1 a 2 b 3 c 4 d 5 e 6 f
(integer) 6
127.0.0.1:6379> zrange scores 0 5
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"

Most popular have highest scores and have highest rank.