Finding values by using partial key name in a Redis Sorted Set

后端 未结 2 840
说谎
说谎 2021-02-10 17:33

I have a sorted set which has the following key name and values :

zrange bargraph:branch:1:category:2:product:4
1) \"76\"
2) \"55\"
3) \"10\"
4) \"84\"
<         


        
2条回答
  •  迷失自我
    2021-02-10 18:14

    Here is an updated answer for 2015.

    If you can upgrade Redis above 2.8, the SCAN command with MATCH will work for this. Before that version, not so much, and do NOT use the KEYS command except in a development environment.

    http://redis.io/commands/scan

    Example on command line:

    $ redis-cli
    127.0.0.1:6379> scan 0 match V3.0:*
    1) "0"
    2) 1) "V3.0:UNITTEST55660BC7E0C5B"
       2) "V3.0:shop.domain.com:route"
       3) "V3.0:UNITTEST55660BC4A2548"
    127.0.0.1:6379> scan 0 match V1.0:*
    1) "0"
    2) (empty list or set)
    127.0.0.1:6379> scan 0 match V3.0:*
    1) "0"
    2) 1) "V3.0:UNITTEST55660BC7E0C5B"
       2) "V3.0:shop.domain.com:route"
       3) "V3.0:UNITTEST55660BC4A2548"
    

    Example in PHP:

    // Initialize our iterator to NULL
    $iterate = null;
    
    // retry when we get no keys back
    $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
    
    while ($arr_keys = $redis->scan($iterate, 'match:*')) {
        foreach ($arr_keys as $str_key) {
            echo "Here is a key: $str_key\n";
        }
        echo "No more keys to scan!\n";
    }
    

    Note, php code is not tested and from the core documentation for example here. Production code would need to be modified depending on the keys needed to look up.

    For those on Ubuntu here are the instructions to upgrade php5-redis:

    1. Download the 2.2.7 package here: http://pecl.php.net/package/redis
    2. $ php -i | grep Redis Redis Support => enabled Redis Version => 2.2.4
    3. Follow instructions in README to phpize, configure, make install
    4. Create a symlink for command line cli package: cd /etc/php5/cli/conf.d && sudo ln -s ../../mods-available/redis.ini 20-redis.ini
    5. $ php -i | grep Redis Redis Support => enabled Redis Version => 2.2.7

提交回复
热议问题