FOSHttpCacheBundle icon indicating copy to clipboard operation
FOSHttpCacheBundle copied to clipboard

[WIP][RFC] Fastly support

Open hpatoio opened this issue 8 years ago • 4 comments

Adding Fastly support. At the moment only tag invalidation. Goes with https://github.com/FriendsOfSymfony/FOSHttpCache/pull/403

hpatoio avatar Jan 19 '18 14:01 hpatoio

will need to be updated once https://github.com/FriendsOfSymfony/FOSHttpCache/pull/451 is wrapped up

dbu avatar Nov 28 '19 12:11 dbu

@hpatoio You ok with doing the fix-ups or would you like us to take over on it?

andrerom avatar Jun 19 '20 07:06 andrerom

An additional issue: since Fastly doesn't use proxy servers for invalidating, the HttpDispatcher's approach of running through proxy servers (done in the fanOut() method) won't work.

A simple solution is to make a copy of the HttpDispatcher class and remove any mentions of servers and baseUris.

Attached example of such a class. FastlyDispatcher.txt

This would just require that the Fastly proxy client is instantiated with the FastlyDispatcher instead:

$some_example_tags = [
  'product1',
  'product2',
];
$httpDispatcher = new \FOS\HttpCache\ProxyClient\FastlyDispatcher();
$options = [
    'service_identifier' => '...',
    'authentication_token' => '...',
    'soft_purge' => true,
];
$fastly = new \FOS\HttpCache\ProxyClient\Fastly($httpDispatcher, $options);
$cacheInvalidator = new \FOS\HttpCache\CacheInvalidator($fastly);
$cacheInvalidator->invalidateTags($some_example_tags)->flush();

proeinfo avatar Oct 29 '20 10:10 proeinfo

Additionally, since Fastly uses space separated surrogate keys, the default comma-separated tag header formatter won't work. Again it may make sense to create a new class for Fastly.

Example implementation:

<?php

namespace FOS\HttpCache\TagHeaderFormatter;

class FastlyTagHeaderFormatter implements TagHeaderFormatter
{
    const FASTLY_TAG_HEADER = 'Surrogate-Key';
    const FASTLY_TAG_HEADER_GLUE = ' ';

    /**
     * @var string
     */
    private $headerName = self::FASTLY_TAG_HEADER;

    /**
     * @var string
     */
    private $glue = self::FASTLY_TAG_HEADER_GLUE;

    /**
     * {@inheritdoc}
     */
    public function getTagsHeaderName()
    {
        return $this->headerName;
    }

    /**
     * {@inheritdoc}
     */
    public function getTagsHeaderValue(array $tags)
    {
        return implode($this->glue, $tags);
    }
}

From there its a simple matter of overriding the service id in the user Symfony application:

services:
    fos_http_cache.tag_handler.header_formatter:
        class: FOS\HttpCache\TagHeaderFormatter\FastlyTagHeaderFormatter

proeinfo avatar Oct 29 '20 10:10 proeinfo