gpt4 book ai didi

php - Redis php逐个处理数组(可能内存不足)

转载 作者:可可西里 更新时间:2023-11-01 11:22:05 26 4
gpt4 key购买 nike

例如,我在 redis 中有一些数据列表,这个列表可能非常大,所以如果我使用getList 方法可能会耗尽内存。

//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);
}
}

我不想从 Redis 获取完整列表,我想逐个获取数据(例如 50 条)。像这样:

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

我怎样才能做到这一点?我可以通过 LLEN 获取列表大小

最佳答案

您可以尝试使用 Rediska_Key_List 的 toArray 方法。它从列表中获取元素。此方法的第一个参数是开始索引,第二个参数是结束索引。来自 the documentation :

array toArray ([integer $start = 0], [integer $end = -1], [ $responseIterator = false], boolean $responseIterator[optional])

integer $start: Start index
integer $end: End index
boolean $responseIterator[optional]: If true - command return iterator which read from socket buffer. Important: new connection will be created
$responseIterator

关于php - Redis php逐个处理数组(可能内存不足),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22451918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com