I am trying to know how many new followers a company have in last 30 days. Below what I have right now:
To add each new follower:
ts = time.time()
redis_store.zadd('followers_companies:'+str(company_id_to_fav), str(current_user.id), ts)
To retrieve all followers:
followers = redis_store.zcard('followers_companies:'+str(company.id))
So, how can I I filter zcard
to retrieve only results from last 30 days?
2
Answers
You can use ZCOUNT to get total number of elements between the min and max. In your case
max
will be+inf
andmin
will be the timestamp value of 30 days ago.For simplicity i assumed 65 if the current timestamp and 35 is the 30 days ago. You may validate it with
ZRANGEBYSCORE
.To be able to do this you need to use timestamp as score. It seems like you are using user ID as score (unless the SDK you’re using has a different order for the options of
ZADD
).Then, you can use
ZRANGEBYSCORE
to retrieve only the last 30 days. You would useZCOUNT
if you only want to know how many, cardinality.