Redis Laravel - strlen() 期望参数 1 为字符串,数组给定

Redis Laravel - strlen() expects parameter 1 to be string, array given

我试图在 Redis 中缓存数据,但出现以下错误:

ErrorException: strlen() expects parameter 1 to be string, array given /home/mktplacemiddleware/demo/vendor/predis/predis/src/Connection/StreamConnection.php:390

以下是我正在尝试的代码:

Redis::set('CacheTest', $finalProduct); //$finalProduct is an Array

我正在使用:

  1. redis 版本 5.5
  2. predis 版本 1.1
  3. php 版本 7.1
  4. laravel 流明 5.7

如有任何帮助,我们将不胜感激。谢谢

您需要将数据存储在 序列化或 json 尝试这样:

Redis::set('CacheTest', json_encode($finalProduct)); //$finalProduct is an Array

并获得你需要解码

的值
$data = Redis::get('CacheTest');
dd(json_decode($data))