Storing an array of data using Redis (from Laravel)(使用 Redis 存储数据数组(来自 Laravel))
问题描述
我已经开始使用 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Redis 存储数据数组(来自 Laravel)


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01