PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to set the key for a cache item with Symfony Cache?

 May 12, 2022     php, symfony, symfony-cache     No comments   

Issue

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;
}

Solution

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:

  • Serialize the result of the whole query and cache that
  • Exclude the ids from you hit on the cache from the second query (remove them from the array $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.


Answered By - yivi
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing