这个 ZINCRBY 语句到底在做什么?
What exactly is this ZINCRBY statement doing?
我正在阅读Josiah Carlson's Redis in Action。
而在第二章中有这样的代码:
conn.zadd('viewed:' + token, {item: timestamp})
conn.zremrangebyrank('viewed:' + token, 0, -26)
conn.zincrby('viewed:', -1, item) # What is this line doing ?
我只是不明白这一行试图做什么:
conn.zincrby('viewed:', -1, item)
它似乎在名为 viewed:
的排序集 (Zset) 中创建一个名为 -1
的成员,并按 item
中指定的值递增它。假设 item
是数字。我不知道你为什么要这样做。
Github link 到代码是 here.
zincrby redis-py中的定义。
我的直觉正确吗?
每个命令我都加了解释,问题的答案在最后
It adds a new item(or update the score) to the specified sorted set, score as timestamp. item
will be going to the at the end of the sorted set since timestamp is current. The rank
of this element will be the highest since it goes to the end of the list with highest score(timestamp).
conn.zadd('viewed:' + token, {item: timestamp})
It removes old items from the sorted set, keeps only most recent 25 items. The recently added item
will not be removed if the size of sorted set is more than 25.
conn.zremrangebyrank('viewed:' + token, 0, -26)
There is also another sorted set(named viewed:
) to keep track of all the items with their corresponding view count. Previous two commands were user(token) specific. This command is decrementing(by one) the view count of an element.
conn.zincrby('viewed:', -1, item)
如果数据建模为将每个项目放入具有默认分数(假设为 1000)的排序集中,并在每次查看时递减。之后,您可能会获得得分最低的最新产品。 (您可以通过递增所有并获得排序的反转来实现它)
我正在阅读Josiah Carlson's Redis in Action。
而在第二章中有这样的代码:
conn.zadd('viewed:' + token, {item: timestamp})
conn.zremrangebyrank('viewed:' + token, 0, -26)
conn.zincrby('viewed:', -1, item) # What is this line doing ?
我只是不明白这一行试图做什么:
conn.zincrby('viewed:', -1, item)
它似乎在名为 viewed:
的排序集 (Zset) 中创建一个名为 -1
的成员,并按 item
中指定的值递增它。假设 item
是数字。我不知道你为什么要这样做。
Github link 到代码是 here.
zincrby redis-py中的定义。
我的直觉正确吗?
每个命令我都加了解释,问题的答案在最后
It adds a new item(or update the score) to the specified sorted set, score as timestamp.
item
will be going to the at the end of the sorted set since timestamp is current. Therank
of this element will be the highest since it goes to the end of the list with highest score(timestamp).
conn.zadd('viewed:' + token, {item: timestamp})
It removes old items from the sorted set, keeps only most recent 25 items. The recently added
item
will not be removed if the size of sorted set is more than 25.
conn.zremrangebyrank('viewed:' + token, 0, -26)
There is also another sorted set(named
viewed:
) to keep track of all the items with their corresponding view count. Previous two commands were user(token) specific. This command is decrementing(by one) the view count of an element.
conn.zincrby('viewed:', -1, item)
如果数据建模为将每个项目放入具有默认分数(假设为 1000)的排序集中,并在每次查看时递减。之后,您可能会获得得分最低的最新产品。 (您可以通过递增所有并获得排序的反转来实现它)