简体   繁体   中英

How can I improve the performance of the Symfony Serializer?

I try to get a json variable from my database:

$table = $this->em->getRepository('App\\Entity\\Data')->findAll();
$encoders = [new JsonEncoder()]; 
$normalizers = [new DateTimeNormalizer(array('datetime_format' => 'd.m.Y')), new ObjectNormalizer($classMetadataFactory)];
$serializer = new Serializer($normalizers, $encoders);

$groupsArray = $serializerGroups->getSerializerGroups($slug);
    $groups = $groupsArray['groups'];
    if( $groups != null) {
      $context['groups'] = $groups;
    }


$output = $serializer->serialize($table, 'json', $context);

dump($output);

The page loads really long until I see the result of output. How can I make it faster?

这实际上取决于您需要的输出它花费的时间太长,因为在我看来,您在 DATA 实体中嵌套了属性以使其更快地使用序列化组https://symfony.com/doc/current/components/serializer.html#component -serializer-attributes-groups-annotations和更快的输出避免使用 findAll你可以使用你的存储库并选择你需要的数据,最后你可以使用缓存组件来存储你的输出一段时间

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