Storing an array of data using Redis (from Laravel)

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


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);

Tags:

Php

Redis

Laravel