I've many items (product_id). Customers (user_id) see the items.
I wish to query which customers seen whatever product within the last 24 hrs. (Quite simply, I wish to keep a listing of user_ids mounted on that product_id...so when 24 hrs expires for any user, that user jumps off that list and also the record vanishes)
How do you store this in Redis? Can someone produce a higher-level schema because I am new in Redis.
For such like I personally use a sorted set with values being user ids and score being the present time. When upgrading the set, remove older products with ZREMRANGEBYSCORE in addition to upgrading time score for that current user.
Update with code:
Each time a new item is added:
ZREMRANGEBYSCORE recentitems 0 [DateTime.Now.AddMinutes(-10).Ticks] ZADD recentitems [DateTime.Now.Ticks] [item.id]
To obtain the ids of products added within the last ten minutes:
ZREVRANGEBYSCORE recentitems [DateTime.Now.Ticks] [DateTime.Now.AddMinutes(-10).Ticks]
Note you could also employ
ZREVRANGE recentitems 0 -1
if you do not mind the set could include older products if nothing continues to be added lately.
That will get you a listing of item ids. After this you use GET/MGET/HGET/HMGET as appropriate to retrieve the particular products for display.
If you would like redis secrets to decrease off instantly then you will most likely desire to use a redis key for each user_id-to-product_id map. So, you'd write by doing something similar to
redis.set "user-to-products:user_id:product_id", timestamp then
redis.expire "user-to-products:user_id:product_id" 86400 (24hrs, within minutes).
To retrieve the present list you need to have the ability to do