I am learning Redis and using an existing app (e.g. converting pieces of it) for practice.
I'm really struggling to understand first
IF
and then (if applicable)
HOW
to use Redis in one particular use-case ... apologies if this is super basic, but I'm so new that I'm not even sure if I'm asking correctly :/
Scenario:
Images
are received by a server and info like
time_taken
and
resolution
is saved in a database entry. Images are then associated (e.g. "belong_to") with one
Event
... all very straight-forward for a RDBS.
I'd like to use a Redis to maintain a list of the 50 most-recently-uploaded image objects for each Event, to be delivered to the client when requested. I'm thinking that a
Sorted Set
might be appropriate, but here are my concerns:
First, I'm not sure if a Sorted Set can/should be used in this associative manner? Can it reference other objects in Redis? Or is there just a better way to do this altogether?
Secondly, I need
the ability
to delete elements that are greater than X minutes old. I know about the
EXPIRE
command for keys, but I can't use this because not all images need to expire at the same periodicity, etc.
This second part seems more like a query on a field, which makes me think that Redis cannot be used ... but then I've read that I could maybe use the Sorted Set
score
to store a timestamp and find "older than X" in that way.
Can someone provide come clarity on these two issues? Thank you very much!
UPDATE
Knowing that the amount of data I need to store for each image is small and will be delivered to the client's browser, can is there anything wrong with storing it in the
member
"field" of a sorted set?
For example
Sorted Set
=>
event:14:pictures <time_taken> "{id:3,url:/images/3.png,lat:22.8573}"
This saves the data I need and creates a rapidly-updatable list of the last X pictures for a given event with the ability to, if needed, identify pictures that are greater than X minutes old ...
First, I'm not sure if a Sorted Set can/should be used in this
associative manner? Can it reference other objects in Redis?
Why do you need to reference other objects? An event may have
n
image objects, each with a
time_taken
and image data; a sorted set is perfect for this. The image_id is the key, the score is time_taken, and the member is the image data as json/xml, whatever; you're good to go there.
Secondly, I need the ability to delete elements that are greater than
X minutes old
If you want to delete elements greater than X minutes old, use
ZREMRANGEBYSCORE
:
ZREMRANGEBYSCORE event:14:pictures -inf (currentTime - X minutes)
-inf
is just another way of saying the oldest member without knowing the oldest members time, but for the top range you need to calculate it based on current time before using this command ( the above is just an example)
https://stackoverflow.com/questions/20361220/redis-sorted-set-store-data-in-member/20364664#20364664
share
improve this answer
–