Search code examples
phptestingredisphpredis

LPUSH function in redis (PHP) throwing bool(false) when I get/dump list


Alright, sooooo... The issue is this: I am LPUSH'ing a variable value to a list called "keys". When I try to get and output the value of that list... it claims the list is empty (bool(false)). The syntax seems correct. This code has worked on other occasions (in fact, I am merely going through each function and testing/refactoring/improving what I have already written). I got snagged on this, and I'm completely boggled. Here's the code (with relevant notes):

$kw = $_REQUEST['keyword']; //we're passing a value to this in a query string

if(empty($kw)){                                 

    $key = 'default';      
    createRedis($key); 

}else{

    $key = $kw;          
createRedis($key);    
}

function{   

    $key = $a;
    $r = new Redis(); 
    $r->connect( 'localhost' );         

    $r->LPUSH( 'keys',$key );   // $key echos a value when one is passed in
    echo $key;                  // a query string, BUT....

    $keys=$r->get('keys');      //'keys'... the redis list
    var_dump($keys);        // throws a bool(false) when dumped     

}

Is there something crazy that I'm missing? Redis is tested on my server as working. I, otherwise, am unable to sort out what the heck is wrong with this. Here's the documentation on LPUSH for phpredis (which is what we are using (it is also installed and working)): https://github.com/nicolasff/phpredis#lpush and the documentation for this on the Redis website (these are CLI examples): http://redis.io/commands/lpush

Any help is sincerely appreciated. Perhaps I am using an ineffective method to test whether or not the redis list 'keys' is retaining value? (That was the whole purpose of the dump).


Solution

  • You have to use lrange instead of get:

    $keys = $r->lrange('keys', 0, -1);