简体   繁体   中英

Doctrine2 Result Cache id

I'm implementing for the first time the Doctrine2 result cache on a project, I've done some tests, but I still have some doubts about setting the cache id.

Assuming that I have a query like this:

$qb->select('o.img,o.title,o.comment')
   ->from('MyBundle:Object', 'o')
   ->where('o.id = :id')
   ->setParameter('id', $id);

$result = $qb->getQuery()
             ->getScalarResult();

I have a changing parameter ( id ): which code is correct to cache the results properly (idk if the result cache manages automatically the queries with parameters)?

First: (I think this)

$result = $qb->getQuery()
             ->useResultCache(true,1800,'my_object_' . $id)
             ->getScalarResult();

Second:

$result = $qb->getQuery()
             ->useResultCache(true,1800,'my_object')
             ->getScalarResult();

Thank you in advance :-)

Actually both of them should work. However, the difference is the name to identify the data stored in cache.

Every cached value will have the following structure:

array(
    "raw sql with parameters",
    array(
        "result 1",
        "result 2",
        "result 3",
    ),
);

Then,

Option 1 : Will be cache the sql and data for the very first MyBundle:Object object loaded in the application. So, if the second object selection have a different id, doctrine will return the previous cached object.

If want to cache an object that does not change in every request, you could use this code. Some examples: a webpage, logged user information, counters, etc.

Option 2 : Will be cache the sql and data for every MyBundle:Object according to its id. If you are intent to load the separated object, you could use this code. Some exaples: a especific MyBundle:Object, a specific specific image, a specific email, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM