使用 Redis 驱动 deleteMultiple 出错
- Langage dominant
- PHP
- Étoiles
- 3
- Forks
- 4
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
在我使用 PhpSpreadsheet 生成电子表格时,我希望使用 Redis 作为缓存类。文档中指出,我可以直接从容器中获取 Psr\SimpleCache\CacheInterface 以得到一个符合PSR-16 的缓存类。按文档操作。
运行时程序抛出了警告错误:
```
PHP Warning: array_map(): Expected parameter 2 to be an array, object given in /Users/splitter/Project/zhentu-oa/vendor/hyperf/cache/src/Driver/RedisDriver.php on line 112
Warning: array_map(): Expected parameter 2 to be an array, object given in /Users/splitter/Project/zhentu-oa/vendor/hyperf/cache/src/Driver/RedisDriver.php on line 112
PHP Warning: Only arrays and Traversables can be unpacked in /Users/splitter/Project/zhentu-oa/vendor/hyperf/cache/src/Driver/RedisDriver.php on line 116
```
和致命错误:
```
Wrong parameter count for Redis::del()[193] in /Users/splitter/Project/zhentu-oa/vendor/hyperf/redis/src/RedisConnection.php
```
查阅源码,发现抛出Warning 的原因是 deleteMultiple 传入了一个 Generator 而非数组。
我尝试以下面方法解决:
```php
public function deleteMultiple($keys)
{
$cacheKeys = array_map(function ($key) {
return $this->getCacheKey($key);
}, array(...$keys));
return (bool) $this->redis->del(...$cacheKeys);
}
```
但是对于致命错误,我不得不重写 deleteMultiple 方法(虽然Redis 中的键的确被删除)
```php
public function deleteMultiple($keys)
{
foreach ($keys as $key) {
$this->redis->del($this->getCacheKey($key));
}
return true;
}
```
我不清楚是我的使用方法不正确或是驱动本身存在缺陷?
环境:
- hyperf/cache:2.2.15
- redis:5.0.6
- redis-ext:5.3.7
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Start with hyperf/cache/src/Driver/RedisDriver.php, especially deleteMultiple around lines 112 and 116, and reproduce the PSR-16 call with a Generator of keys. Check the reported array_map, unpacking, and Redis::del failures. Done means deleteMultiple accepts the documented input, removes the keys, and completes without warnings or a fatal error.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- php, redis
- Domaine
- backend, databases
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 38/100