优化 Symfony API 中默认值的响应过滤(例如 isActive)

问题描述 投票:0回答:1

寻求有关在 Symfony 中优化响应过滤的指导,重点是实现默认值,例如将 isActive 设置为 true。尽管付出了努力,我当前的解决方案的性能仍然不佳。我使用了控制器,但数据检索非常耗时,需要规范化器来生成响应。我正在寻找更有效的策略或代码模式的见解,以加速响应渲染。

这是一个示例场景:

namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\MasterList;

class MasterListController extends AbstractController
{
    /**
     * @Route("/api/master_lists", name="get_active_master_lists", methods={"GET"})
     */
    public function getActiveMasterLists(SerializerInterface $serializer)
    {
        $repository = $this->getDoctrine()->getRepository(MasterList::class);

        // Use custom repository method to retrieve only active MasterLists
        $activeMasterLists = $repository->findActiveMasterLists();

        // Use Symfony Serializer to convert entities to array
        $serializedMasterLists = $serializer->normalize($activeMasterLists, null, [AbstractNormalizer::IGNORED_ATTRIBUTES => ['masterList']]);

        // Return the serialized data as a JSON response
        return new JsonResponse($serializedMasterLists);
    }
}

目前响应时间为5000MS,对于数据量来说已经很高了。我已经实现了分页,对 isActive 列进行了索引,并尝试了缓存。客户端更喜欢单个响应以避免循环浏览多个页面。您将如何以不同的方式处理此问题以提高性能?

api symfony api-platform.com
1个回答
0
投票

这不是我问题的答案,但它是一个开始。我分享这个是希望我能得到更好的替代方案的帮助。

class MasterListController extends AbstractController
{
    /**
     * @Route("/api/master_lists", methods={"GET"})
     */
    public function getMasterLists(Request $request, SerializerInterface $serializer)
    {
        // Get query parameters with default values
        $isActive = $request->query->get('isActive', true);

        // Your logic goes here using $isActive
        // For example, fetching data from the repository
        $masterLists = $this->getDoctrine()->getRepository(MasterList::class)->findByIsActive($isActive);

        // Manually build the normalized data array
        $normalizedData = [];
        foreach ($masterLists as $masterList) {
            $normalizedData[] = [
                '@id' => '/api/master_lists/'.$masterList->getId(),
                '@type' => 'MasterList',
                'id' => $masterList->getId(),
                'value' => $masterList->getValue(),
                'label' => $masterList->getLabel(),
                'isActive' => $masterList->getIsActive(),
                'description' => $masterList->getDescription(),
                'createdAt' => $masterList->getCreatedAt()->format(\DateTime::RFC3339),
                'createdBy' => $masterList->getCreatedBy(),
                'orderBy' => $masterList->getOrderBy(),
                'listtype' => intval($masterList->getListType()->getId()),
            ];
        }

        // Use API Platform serialization groups for the response
        $data = [
            'hydra:member' => $normalizedData,
            'hydra:totalItems' => count($masterLists),
            'hydra:view' => [
                // Add the relevant properties for view links
            ],
            'hydra:search' => [
                // Add the relevant properties for search
            ],
        ];

        return $this->json($data);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.