Laravel 和 redis 扫描

标签 laravel laravel-5 redis laravel-5.2

我正在尝试将 redis scan 与 laravel 一起使用。我可以发出一个返回 10 个键的请求,但我希望循环直到返回所有键。我不确定如何用 laravel 做到这一点。目前我有

$test = Redis::scan(0, 'match', '*keypattern*');

我不知道是否有“laravel”方法可以做到这一点。

编辑:

我使用 composer 导入 predis/predis 并让它与

use Predis\Collection\Iterator;
use Predis;

...

$client = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => 'localhost',
    'port'   => 6379,
]);

foreach (new Iterator\Keyspace($client, '*keypattern*') as $key) {
     $arr[] = $key;
}

但我想知道 laravel 的方式

编辑:

单个Redis::scan的var_dump

array(2) {
  [0]=>
  string(4) "23"
  [1]=>
  array(10) {
    [0]=>
    string(19) "key17"
    [1]=>
    string(19) "key72"
    [2]=>
    string(76) "key11"
    [3]=>
    string(19) "key73"
    [4]=>
    string(19) "key63"
    [5]=>
    string(19) "key87"
    [6]=>
    string(19) "key70"
    [7]=>
    string(19) "key65"
    [8]=>
    string(19) "key82"
    [9]=>
    string(19) "key43"
  }
}

最佳答案

谢谢@martinczerwi 这是一个非递归版本:

function scanAllForMatch($pattern)
{
    $cursor = 0;
    do {
        list($cursor, $keys) = Redis::scan($cursor, 'match', $pattern);
        foreach ($keys as $key) {
            yield $key;
        }
    } while ($cursor);
}

关于Laravel 和 redis 扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477172/

相关文章:

transactions - 在 Redis 中,当存在没有关联的 EXEC 或 DISCARD 的 MULTI 时会发生什么?

使用 2 个或更多方法时,Laravel 中的 PHPUnit 测试失败

php - Laravel:验证请求中的空数组

javascript - TokenMismatchException laravel 5.3

azure - Azure 上的 Redis 连接断开

Redis删除排序集中的项目

php - strtotime 在转换日期字符串时返回 false

laravel 5 : what is the purpose of service and providers directory

laravel - 多个 id 的 UpdateExistingPivot

php - 在 laravel eloquent 中一起使用 with 和 withCount