Search code examples
phpredislarge-data

Redis php processing array part by part (may run out of memory)


For example i have list in redis with some data, this list can be really large so if i use getList method i can run out of memory.

//getList
$Data = Rediska()->getList('example_list_of_data');
foreach ($Data as $value) {
    if (in_array($someValue, $value)) {
        //delete element from list
        Rediska()->popFromList('example_list_of_data', $value);
    }
}

I dont want to get full list from Redis, i want to get data part by part(for exmaple of 50 pieces). Like so:

   get 50 first items
   ->try to find element
   ->if so delete it and stop iteration
   ->if did not find iterate next 50 etc...

how can i achive this? i can get list size with LLEN


Solution

  • i have found solution, get list part by part, tested it with microtime and memory usage. It takes a little bit more time, but much less memory on list with 1000000 records.

    public static function processRedisList($Start, $End, $ListName
        $ElementToFind, $Found = false, $Part
    ){
       while($Found != true) {
         //
         $Data = NoSQL_Singleton::Rediska()->getList($ListName, $Start, $End);
         if (!empty($Data)) {
            foreach ($Data as $Key => $Value) {
               //process List here
               //do what you want
               $Found = true;
               return $Found;
            }
          $Start = $Start + $Part;
          $End = $End + $Part;
        }
       }
       return false;
    }