How to use SCAN with the MATCH option in Predis

前端 未结 2 1941
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 01:08

I have previously used the KEYS command to search for keys matching a certain pattern in my Redis database. Since Redis 2.8, the SCAN command seems to be preferred over KE

相关标签:
2条回答
  • 2021-02-02 02:02

    I found how to do it in the Predis examples directory.

    To use SCAN to search for matching keys in a database, you simply use the Predis\Collection\Iterator\Keyspace class:

    use Predis\Collection\Iterator;
    
    $client = ...;
    $pattern = 'foo*';
    
    foreach (new Iterator\Keyspace($client, $pattern) as $key) {
        ...
    }
    

    Apparently Predis has an iterator class in Predis\Collection\Iterator for each of the commands that return iterators:

    • Keyspace for SCAN
    • HashKey for HSCAN
    • SetKey for SSCAN
    • SortedSetKey for ZSCAN
    • ListKey for LRANGE - This doesn't really use Redis iterators, but it's a nice interface to LRANGE anyway.
    0 讨论(0)
  • 2021-02-02 02:05

    Maybe this is helpful for other Predis beginners and you're coming from a PHP/MySQL background like me you can use this:

    foreach (new Iterator\HashKey($client, $pattern) as $index => $value) {
        ...
    }
    

    When you previously generated an array dataset with $client->hmset($index, $array).

    0 讨论(0)
提交回复
热议问题