Home » Php » Storing an array of data using Redis (from Laravel)

Storing an array of data using Redis (from Laravel)

Posted by: admin October 29, 2017 Leave a comment


I have started to work with laravel. It is quite interesting to work. I have started to use the features of laravel. I have started to use redis by install redis server in my system and change the configuration for redis in app/config/database.php file. The redis is working fine for the single variables by using set. i.e.,

$redis = Redis::connection();

$redis->set('name', 'Test');

and i could able to get the value by using


But i want to set the array by using set function. If i try do that getting the following error

 strlen() expects parameter 1 to be string, array given 

I have tried by using following codes.

$redis->set('name', array(5, 10));

$values = $redis->lrange('names', array(5, 10));

and if i use

$values = $redis->command('lrange', array(5, 10));

getting the following error

 'command' is not a registered Redis command 

Can any one explain me the problem and is that possible with redis?…we can set the array values using redis ?


This has been answered in the comments but to make the answer clearer for people visiting in the future.

Redis is language agnostic so it won’t recognise any datatype specific to PHP or any other language. The easiest way would be to serialise / json_encode the data on set then unserialise/json_decode on get.

Example to store data using json_encode:

$redis = Redis::connection();

$redis->set('user_details', json_encode(array('first_name' => 'Alex', 
                                              'last_name' => 'Richards'

Example to retrieve data using json_decode:

$redis    = Redis::connection();
$response = $redis->get('user_details');

$response = json_decode($response);

Leave a Reply

Your email address will not be published. Required fields are marked *