Storing an array of data using Redis (from Laravel)

51,818

Solution 1

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:

use Illuminate\Support\Facades\Redis;

$redis = Redis::connection();

$redis->set('user_details', json_encode([
        'first_name' => 'Alex', 
        'last_name' => 'Richards'
    ])
);

Example to retrieve data using json_decode:

use Illuminate\Support\Facades\Redis;

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

$response = json_decode($response);

Solution 2

Redis supports multiple data structure types like hash, link list and array.

But you have to use proper methods from Laravel Redis facade like below:

// set user_details --redis_key-- and --redis_value-- as array of key_values 
Redis::hmset('user_details',["firstName" => "Foo", "lastName" => "Bar"]);

// get all as an associative array
Redis::hgetall('user_details');

// get just the keys as an array
Redis::hkeys('user_details');

Further information: https://redis.io/commands#hash

Share:
51,818

Related videos on Youtube

Kalaiyarasan
Author by

Kalaiyarasan

Web developer having experience in Angular Ionic Html css Javascript and Jquery Laravel PHP Mysql Wordpress Redis Codeigniter MVC framework Mediawiki Wowza media server Amazon EC2 Amazon S3 Smarty Media temple server Customising Flex video applications using AS3 Kaltura video editor installation and usage CS Cart with good problem-solving skills.

Updated on April 29, 2021

Comments

  • Kalaiyarasan
    Kalaiyarasan about 3 years

    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

    $redis->get('name');
    

    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 ?

    • Mark Baker
      Mark Baker about 10 years
      Redis isn't familiar with PHP non-scalar datatypes like arrays, but quick and dirty option would be $redis->set('name', serialize(array(5, 10))); or $redis->set('name', json_encode(array(5, 10))); and then unserialize or json_decode on get
    • Kalaiyarasan
      Kalaiyarasan about 10 years
      So we have to encode it not other choice to set directly as an array?
    • Mark Baker
      Mark Baker about 10 years
      A PHP array is a datatype specific to PHP, redis is language agnostic, so it has no idea how to handle it... personally I use Redis with igbinary and automatic serialization
    • Kalaiyarasan
      Kalaiyarasan about 10 years
      Ok thanks for your explanation mark
    • Abilogos
      Abilogos about 3 years
      @Kalai : you can store it as an associate-array without serializing, using laravel facade which i have mentioned in my answer.
  • Tw Bert
    Tw Bert about 10 years
    Correct answer. MsgPack is also a good (language agnostic) encoding format. As a sidenote, this also applies to server-side Lua scripting. See here.
  • DaAmidza
    DaAmidza over 5 years
    Are you aware how much time this will require until you decode and encode everything?
  • ajtrichards
    ajtrichards over 5 years
    @DaAmidza This is like four years old now... there's probably a much more performant way to do this. Also, this answer made it as simple as possible for the OP (and others to understand)
  • Techdive
    Techdive about 5 years
    @ajtrichards .. i tried but i am getting invalid arguments error . 127.0.0.1:6379> set('user_details', json_encode(array('first_name' => 'Alex', 'last_name' => 'Richards'))); Invalid argument(s)
  • Akash lal
    Akash lal almost 5 years
    use Redis; at the top of file.