我正在尝试将缓存添加到现有的 Symfony 项目中。但我不确定如何最好地进行我得到一个带有 id 的数组。如果缓存中有项目,我会检查每个 id。如果没有,则中断并向数据库发送查询。然后我会得到结果,现在我可以将这些值存储在我的缓存中。但我不知道如何将 id 设置为键
public function getHelpfulResult(array $reviewIds): array
{
$helpfulCache = new FilesystemAdapter("helpful", 2 * 60 * 60, "cache");
$helpfulValues = [];
foreach ($reviewIds[0] as $id) {
$item = $helpfulCache->getItem((string)$id);
if($item->isHit()) {
array_push($helpfulValues, $item);
} else {
break;
}
}
$repo = $this->getDoctrine()->getRepository('Project:Test\Helpful', 'reviews');
$query = $repo->createQueryBuilder('helpful')
->where("helpful.parentId IN (:parentIds) AND helpful.type = 'review'")
->setParameter('parentIds', $reviewIds)
->getQuery();
$result = $query->getResult();
foreach($result as $item) {
$cache->set($item);
}
$cache->save();
return $result;
}