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

Storing an array of data using Redis (from Laravel)(使用 Redis 存储数据数组(来自 Laravel))
本文介绍了使用 Redis 存储数据数组(来自 Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已经开始使用 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)