使用Redis存储数据数组(来自Laravel)

时间:2022-10-22 20:55:45

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.,

我已经开始使用laravel了。工作很有意思。我已经开始使用laravel的功能。我已经开始在我的系统中通过安装redis服务器使用redis,并在app / config / database.php文件中更改redis的配置。通过使用set,redis对单个变量工作正常。即,

$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

但我想使用set函数设置数组。如果我尝试这样做会得到以下错误

 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 ?

任何人都可以解释我的问题,并且可以使用redis吗?...我们可以使用redis设置数组值吗?

1 个解决方案

#1


35  

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.

Redis与语言无关,因此它无法识别PHP或任何其他语言特有的任何数据类型。最简单的方法是对set上的数据进行序列化/ json_encode,然后在get上unserialise / json_decode。

Example to store data using json_encode:

使用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:

使用json_decode检索数据的示例:

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

$response = json_decode($response);

#1


35  

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.

Redis与语言无关,因此它无法识别PHP或任何其他语言特有的任何数据类型。最简单的方法是对set上的数据进行序列化/ json_encode,然后在get上unserialise / json_decode。

Example to store data using json_encode:

使用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:

使用json_decode检索数据的示例:

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

$response = json_decode($response);