1

I have a dynamic array of tasks objects with reminder dates.

I have an idea to use a redis sorted set to implement a reminders "sorted" queue. The idea is to add tasks' reminder dates to a redis sorted set and periodically run checking if there are reminders in the set relevant to checking date/time(the current date/time for example).

My tool for operating redis is the Predis client. I put reminders in the set in the following way:

$redis->zadd('reminders', [strtotime($task->reminder_at) => $task->id]);  

To retreive the reminders that earlier than the current timestamp, I try to get them in the following way:

$currentTime = time();
$reminders = $redis->zrangebyscore('reminders', '-inf', $currentTime, ['withscores' => true]); 

But I get all the reminders, no matter if they are earlier than the current timestamp or not. I think it is maybe because of not correct comparison, but I can't understand why does it happen. Is is possible at all to use timestamps as scores in this case? I would like to get all the reminders, that are earlier or equal to the current timestamp(Or DateTime. No matter). What is wrong with my way of using a sorted set?

Evg127
  • 53
  • 1
  • 6

1 Answers1

2

I am not a Redis expert/developer but the base idea is usually search works with array values

You are trying to search based on the timestamp into the array but that array contains timestamps as keys, not values. So your code is not working

Reverse the order and it will start working

$redis->zadd('reminders', [$task->id => strtotime($task->reminder_at)]);
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    Yes, you are right. Thanks! It works like an ordinary array. And the redis serches by values. But what if I would like to store in a reminder's entry something more complicated than just a pair id->timestamp but keeping serching key as a reminder date? In what way is it possible to pass more data in a reminder entry in the sorted set? For example, if I want to keep sorting by the reminders date, but I want to store in the reminder entry not only id but its userId as well? All these ids are present in each task's object. – Evg127 May 02 '23 at 09:31
  • 1
    if timestamp still saved as values then `array_search(,array_column($your_array,'timestamp index name'))`. As i said I am not an expert so better to ask a new question. – Alive to die - Anant May 02 '23 at 09:43