我有一个使用FOSREST包的REST API。响应由JMS Bundle序列化。现在我想缓存一些响应,因为序列化需要相当长的时间。我在考虑在序列化之后存储响应数据,然后,如果数据库没有更改,则交付缓存的响应,而不是再次执行序列化。有什么办法可以做到这一点吗?
更具体地说:我有一个控制器,它输出大量的数据,这些数据在大多数情况下都是不变的。由于序列化需要几秒钟的时间,所以我希望使用缓存的输出来加快速度。
我想我需要设置一个事件"afterSerialization“来写缓存。但是,因为fosrestBundle被配置为序列化视图处理的所有数据,所以我找不到一种方法来输出预先序列化的数据,而不需要再次发送到序列化程序。
public function postProductsAction(Request $request)
{
    $em = $this->get('doctrine')->getManager();
    // TODO check if db is unchanged and cache exists
    // if check is true stop here and redirect to cached json response
    $products = $em->getRepository('PPApiBundle:Produkte')->findBy(array("status" => 1));
    $data = array(
      "data" => $products,
      "opt" => array()
    );
    $view = $this->view($data, 200);
    $ret = $this->handleView($view);
    // TODO cache json string after serialization 
    return $ret
}发布于 2018-01-25 03:13:08
在序列化之前,您可以使用doctrine查询构建器的查询缓存和响应缓存方法来缓存结果。
->getQuery()->useQueryCache(true)->useResultCache(true, 3600)https://stackoverflow.com/questions/48426099
复制相似问题