I am trying to add caching to an existing Symfony project. But I am unsure how best to proceed I get an array with ids. I check every id if there is an item in the cache. If not, then break and send a query to the database. Then I'll get the result and now I can store these values in my cache. But I have no idea how to set the id as a key
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;
}
Regarding the basic: "how do I set the 'id' for a cache item" question:
You basically don't. You try to retrieve a cache item by an ID, and then the object you have already has that ID, was it hit or not.
You save it, and a new cache item with ID "x" exists now.
E.g.
$cacheItem = $cacheyAdapter->getItem('some-arbitrary-id');
if (!$cacheItem->isHit()) {
$cacheItem->set('some new value');
$cacheAdapter->save($cacheItem);
}
With the above code you'd check if some-arbitrary-id
existed in the cache layer, and if it didn't set a value for it and stored it back.
Regarding the rest of the code in the question, I'm afraid the whole thing is rather poorly designed, with a single element being stale on the cache forcing you to make the whole query again and retrieve everything from the DB.
I'm not going to spend time on that, since it's out of scope for this question (and too broad and opinionated for SO), some basic ideas:
$reviewIds
). Also, keep the non-hit cache items in another array so that when you get them from the DB you update those in the cache layer. Finally merge results from cache and DB before returning.