core icon indicating copy to clipboard operation
core copied to clipboard

Varnish invalidation on subresource

Open GHuygen opened this issue 1 year ago • 2 comments

API Platform version(s) affected: 3.3.6

Description
Varnish cache is not released on a subresource. For example purposes, I use a Supplier and a DeliveryDay entity.

Supplier:

#[ORM\Entity(repositoryClass: SupplierRepository::class)]
#[ApiResource(
    operations: [
        new Post(),
        new Get(),
        new GetCollection(),
        new Put(),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['supplier:read']],
    denormalizationContext: ['groups' => ['supplier:write']],
)]
class Supplier
{

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(groups: ['supplier:read'])]
    private ?int $id = null;

    #[ORM\Column(length: 128)]
    #[Groups(groups: ['supplier:read', 'supplier:write'])]
    #[Assert\NotBlank]
    #[Assert\Length(max: 128)]
    public string $name = '';

    #[ORM\OneToMany(targetEntity: DeliveryDay::class, mappedBy: 'supplier', orphanRemoval: true)]
    private Collection $deliveryDays;

    public function __construct()
    {
        $this->deliveryDays = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return Collection<int, DeliveryDay>
     */
    public function getDeliveryDays(): Collection
    {
        return $this->deliveryDays;
    }

    public function addDeliveryDay(DeliveryDay $deliveryDay): static
    {
        if (!$this->deliveryDays->contains($deliveryDay)) {
            $this->deliveryDays->add($deliveryDay);
            $deliveryDay->setSupplier($this);
        }

        return $this;
    }

    public function removeDeliveryDay(DeliveryDay $deliveryDay): static
    {
        if ($this->deliveryDays->removeElement($deliveryDay)) {
            // set the owning side to null (unless already changed)
            if ($deliveryDay->getSupplier() === $this) {
                $deliveryDay->setSupplier(null);
            }
        }

        return $this;
    }
}

DeliveryDay:

#[ORM\Entity]
#[ApiResource(
    operations: [
        new Post(),
        new Get(),
        new GetCollection(
            uriTemplate: '/suppliers/{id}/delivery_days',
            uriVariables: [
                'id' => new Link(
                    fromProperty: 'deliveryDays',
                    fromClass: Supplier::class
                ),
            ]
        ),
        new Put(),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['delivery_day:read']],
    denormalizationContext: ['groups' => ['delivery_day:write']],
)]
class DeliveryDay
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(groups: ['delivery_day:read'])]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'deliveryDays')]
    #[ORM\JoinColumn(nullable: false)]
    #[Groups(groups: ['delivery_day:read', 'delivery_day:write'])]
    private ?Supplier $supplier = null;

    #[ORM\Column(length: 64)]
    #[Groups(groups: ['delivery_day:read', 'delivery_day:write'])]
    #[Assert\NotBlank]
    #[Assert\Length(max: 64)]
    public string $day = '';

    #[ORM\Column(length: 64)]
    #[Groups(groups: ['delivery_day:read', 'delivery_day:write'])]
    #[Assert\NotBlank]
    #[Assert\Length(max: 64)]
    public string $region = '';

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getSupplier(): ?Supplier
    {
        return $this->supplier;
    }

    public function setSupplier(?Supplier $supplier): static
    {
        $this->supplier = $supplier;

        return $this;
    }
}

How to reproduce
If you now use /suppliers/{id}/delivery_days, Varnish caches that response. If you then make a POST request to /delivery_days, the cache from the collection route is not invalidated.

Possible Solution
If you add a default GetCollection:

new GetCollection(),
new GetCollection(
    uriTemplate: '/suppliers/{id}/delivery_days',
    uriVariables: [
        'id' => new Link(
            fromProperty: 'deliveryDays',
            fromClass: Supplier::class
        ),
    ]
),

It works, but then there is a route exposed that you don't really want.

So, I would suggest always adding a default cache tag to a collection. In this case, that would be /delivery_days (even when the route doesn't exist) and also add this tag to the invalidation service (even if the route doesn't exist).

end word If I'm doing something that is not intended or missed some docs, please point me in the right direction. I would like to help resolve this issue by contributing, but I have never done that before. I would appreciate some help or guidance to get started.

GHuygen avatar Jul 09 '24 09:07 GHuygen

For our application, we have overriden PurgeHttpCacheListener class (for various reason). The specific issue you're describing, we've solved by iterating over all existing GetCollection operations of a resource class, hence invalidating all routes in which the specific entity is embedded.

See this code line and the loops around it: https://github.com/ecamp/ecamp3/blob/devel/api/src/HttpCache/PurgeHttpCacheListener.php#L181

By the way, the invalidation is not only needed for POST request, but also for DELETE requests and for updates as well, for example in case the $supplier of a DeliveryDay entity is modified.

I anyway wanted to check, which modifications we have done on our side which I could easily contribute back to api-platform. So I can try to to open a PR for this.

usu avatar Jul 12 '24 15:07 usu

i think its related https://symfony-devs.slack.com/archives/C39FKU9AL/p1723577619039279

g-ra avatar Aug 23 '24 08:08 g-ra

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 24 '24 17:10 stale[bot]