Easywechat 4.x 替换Redis缓存无效

问题描述

Easywechat 4.x 支持自定义缓存

use Symfony\Component\Cache\Adapter\RedisAdapter;

// 创建 redis 实例
$client = new \Predis\Client('tcp://10.0.0.1:6379');

// 创建缓存实例
$cache = new RedisAdapter($client);

// 替换应用中的缓存
$app->rebind('cache', $cache);

当本地测试环境和生产环境均适用同一个Redis 配置后,发现本地和生产环境无法相互正确读取缓存内容。例如提示`'Credential "component_verify_ticket" does not exist in cache.'`

开始以为是 redis 配置上出了问题,结果是坑在组件`Symfony\Component\Cache` 里

坑在Symfony Cache 组件

查看Symfony/Cache文档,有章节提到了数据序列化的问题。默认的序列化是采用 php 自带的 `serialize()` ,但若检测到安装了`igbinary`扩展,会使用扩展的序列化函数来替代 。

由于服务器的 PHP 安装了`igbinary`扩展,而本地没有,所以两者序列化和反序列化方式不一致,以此加载数据出现异常了。

解决办法

统一两个环境中的 php 扩展,要么同时删掉 igbinary 扩展,要们同时安装即可。