Update Doctrine Cache Values in Symfony

筅森魡賤 提交于 2019-12-13 19:27:00

问题


I have a Symfony Standard Distribution app and use Doctrine's query builder and result cache to speed up database queries. I also assign unique ids to all my caches such as

....

->getQuery()
->useResultCache(true, 2592000, 'single_product_query_id_'.$id)
->getOneOrNullResult();

....

When a product field changes I can delete this specific cache using

....

$em = $this->getDoctrine()->getManager();
$cacheDriver = $em->getConfiguration()->getResultCacheImpl();
$cache = $cacheDriver->fetch('single_product_query_id_'.$id);
if($cache){
    $cacheDriver->delete('single_product_query_id_'.$id);
}

....

Deleting the cache obviously make the changes visible instantly but I found this method to be a waste because in my controller I already have the up-to-date data to be persisted in my db using doctrine so I resolved to updating my cache instead of deleting it. This is where my challenge lies because the documentation is not clear on how to do this apart from the code to save the cache which is

....

$cacheDriver->save('single_product_query_id_'.$id, $new_cache, $new_lifetime);

....

So I dug deeper by passing my cache as variable $cache to a twig template and viewing via var dump. It looked like this

....

array (size=2)
  'SELECT d0_.id AS id_0, d0_.title AS title_1, d0_.imagepath AS imagepath_2, d0_.description AS description_3,.......................
 array (size=1)
      0 => 
        array (size=57)
          'id_0' => string '1' (length=1)
          'title_1' => string ''Tasty Thursday'!' (length=17)
          'imagepath_2' => string 'main.jpeg' (length=9)
          'description_3' => string 'this and every Thursday, buy a 2kg forest cake and get a 1kg fruity forest cake FREE!!!' (length=87)

...........................

I do not know how doctrine generates this multidimensional array, I would like to get this function(s) so that I can properly populate my cache to update.

Currently, I successfully update my cache by var dumping my cache to find out which row I need to update then iterating through the array. But every time I get a new idea and add a new row to an entity I have to repeat this process especially if this entity was joined to product table.

So my question is, how can I manually build a doctrine cache array from query result or better entity?

来源:https://stackoverflow.com/questions/31365870/update-doctrine-cache-values-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!