我已经开始使用 Laravel.工作很有趣.我已经开始使用 Laravel 的功能了.我已经开始使用 redis
通过在我的系统中安装 redis 服务器并更改 app/config/database.php
文件中的 redis 配置.通过使用 set
,redis 可以很好地处理单个变量.即,
$redis = Redis::connection();$redis->set('name', 'Test');
我可以通过使用获得价值
$redis->get('name');
但我想使用 set
函数来设置数组.如果我尝试这样做,则会出现以下错误
strlen() 期望参数 1 是字符串,给定数组
我已尝试使用以下代码.
$redis->set('name', array(5, 10));$values = $redis->lrange('names', array(5, 10));
如果我使用
$values = $redis->command('lrange', array(5, 10));
出现以下错误
'command' 不是注册的 Redis 命令
任何人都可以向我解释这个问题吗?redis 可以吗?...我们可以使用 redis
设置数组值吗?
这已经在评论中回答了,但为了让以后访问的人更清楚地回答.
Redis 与语言无关,因此它不会识别特定于 PHP 或任何其他语言的任何数据类型.最简单的方法是 serialise
/json_encode
数据集,然后 unserialise
/json_decode
获取.>
使用 json_encode
存储数据的示例:
使用 IlluminateSupportFacadesRedis;$redis = Redis::connection();$redis->set('user_details', json_encode(['first_name' =>'亚历克斯','姓氏' =>理查兹"]));
使用 json_decode
检索数据的示例:
使用 IlluminateSupportFacadesRedis;$redis = Redis::connection();$response = $redis->get('user_details');$response = json_decode($response);
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
?
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 IlluminateSupportFacadesRedis;
$redis = Redis::connection();
$redis->set('user_details', json_encode([
'first_name' => 'Alex',
'last_name' => 'Richards'
])
);
Example to retrieve data using json_decode
:
use IlluminateSupportFacadesRedis;
$redis = Redis::connection();
$response = $redis->get('user_details');
$response = json_decode($response);
这篇关于使用 Redis 存储数据数组(来自 Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!