使用 Predis,如何设置(即存储)多维关联数组? [英] Using Predis, how to SET (i.e. store) a multidimensional associative array?

查看:55
本文介绍了使用 Predis,如何设置(即存储)多维关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循本指南开始使用PHP 中的 Predis.在本指南中,他们提供了 set() 函数来存储键值对:

<块引用>

//设置消息包含Hello world"$redis->set(';message';, ';Hello world';);

现在我想使用 predis 缓存的数据是一个来自 MongoDB 数据库的多维关联数组,它看起来像

allRowsDataArray = array(row0 = 数组(键 0 = 值 0,键 1 = 值 1,...很快),行 1 = 数组(字段 0 = 值 0,字段 1 = 值 1,...很快),...很快)

所以为了将这个数组保存在缓存中,当我这样做时

$redis->set('allRowsDataArray', $allRowsDataArray);

我收到此异常/错误:

警告:strlen() 期望参数 1 为字符串,数组在/var/www/html/testProject/plugins/predis/predis/src/Connection/StreamConnection.php 第 390 行中给出注意:第 391 行/var/www/html/testProject/plugins/predis/predis/src/Connection/StreamConnection.php 中的数组到字符串转换致命错误:未捕获的 Predis\Response\ServerException:ERR 协议错误:/var/www/html/testProject/plugins/predis/predis/src/Client.php:370 中的无效批量长度:#0/var/www/html/testProject/plugins/predis/predis/src/Client.php(335): Predis\Client->onErrorResponse(Object(Predis\Command\StringSet), Object(Predis\Response\Error)) #1/var/www/html/testProject/plugins/predis/predis/src/Client.php(314): Predis\Client->executeCommand(Object(Predis\Command\StringSet)) #2/var/www/html/testProject/plugins/predis/index.php(110): Predis\Client->__call('set', Array) #3 {main} 扔在/var/www/html/testProject/plugins/predis/predis/src/Client.php 在线 370

所以问题是我错过了什么?我应该如何解决这个问题?

解决方案

Set 方法期望值为字符串.使用 json_encode() 保存数据.

$redis->set('allRowsDataArray', json_encode($allRowsDataArray));

并使用 json_decode()Redis 中检索.

I am following this guide to get started with Predis in PHP. In this guide, they have given the set() function to store key value pairs:

//sets message to contain "Hello world"
$redis->set(';message';, ';Hello world';);

Now the data I want to cache using predis is a multidimensional associative array coming from a MongoDB database, and it looks like

allRowsDataArray = array(
  row0 = array(
    key0 = value0,
    key1 = value1,
    ...so on
  ),
  row1 = array(
    field0 = value0,
    field1 = value1,
    ...so on
  ),
  ...so on
)

So in order to save this array in cache, when I do

$redis->set('allRowsDataArray', $allRowsDataArray);

I get this exception/error:

Warning: strlen() expects parameter 1 to be string, array given in /var/www/html/testProject/plugins/predis/predis/src/Connection/StreamConnection.php on line 390
Notice: Array to string conversion in /var/www/html/testProject/plugins/predis/predis/src/Connection/StreamConnection.php on line 391
Fatal error: Uncaught Predis\Response\ServerException: ERR Protocol error: invalid bulk length in /var/www/html/testProject/plugins/predis/predis/src/Client.php:370 Stack trace: #0 /var/www/html/testProject/plugins/predis/predis/src/Client.php(335): Predis\Client->onErrorResponse(Object(Predis\Command\StringSet), Object(Predis\Response\Error)) #1 /var/www/html/testProject/plugins/predis/predis/src/Client.php(314): Predis\Client->executeCommand(Object(Predis\Command\StringSet)) #2 /var/www/html/testProject/plugins/predis/index.php(110): Predis\Client->__call('set', Array) #3 {main} thrown in /var/www/html/testProject/plugins/predis/predis/src/Client.php on line 370

So the question is that what am I missing? How should I resolve this problem?

解决方案

Set method expect value to be string. Use json_encode() to save data.

$redis->set('allRowsDataArray', json_encode($allRowsDataArray));

And use json_decode() to retrieve from Redis.

这篇关于使用 Predis,如何设置(即存储)多维关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆