Search code examples
phpredispredis

how to get predis data with offset and limit?


i stored my data as encoded data in redis using predis hset library. so the data is stored in one of the db in a hash name like e.g myHash

field = integer
value = encoded data..

e.g

1    {'pk_id':1,'name' : 'test1'}
2    {'pk_id':2,'name' : 'test2'}
3    {'pk_id':3,'name' : 'test3'}
...and so on...

there were like 400k+ rows of integer field with their encoded data. is there a way to pull these datas with offset and limit ? because if i do e.g

$predisObj->hgetall('myHash');

it pulls out everything and the browser crashes because of too many data


Solution

  • predis shares the command list with redis as it is a php interface for redis store. Therefore you can use the standard redis commands to fulfill your needs:

    The best way to achieve what you want is SCAN command From predis documentation:

    // === Keyspace iterator based on SCAN ===
    echo 'Scan the keyspace matching only our prefixed keys:', PHP_EOL;
    foreach (new Iterator\Keyspace($client, 'predis:*') as $key) {
        echo " - $key", PHP_EOL;
    }
    

    This command will return an iterator thus the memory issues will not be of importance anymore.