Pay attention to share storage between application with caution!
For example, a response with cache-control header "private, max-age=60"
will be cached by this strategy.
The rules applied are from RFC 7234.
public function testCacheProvider() { $TMP_DIR = __DIR__ . '/tmp/'; $cacheProviders = [new DoctrineCacheStorage(new ArrayCache()), new DoctrineCacheStorage(new ChainCache([new ArrayCache()])), new DoctrineCacheStorage(new FilesystemCache($TMP_DIR)), new DoctrineCacheStorage(new PhpFileCache($TMP_DIR)), new FlysystemStorage(new Local($TMP_DIR))]; $request = new Request('GET', 'test.local'); $response = new Response(200, ['Cache-Control' => 'max-age=60'], 'Test content'); /** @var CacheProvider $cacheProvider */ foreach ($cacheProviders as $cacheProvider) { $this->rrmdir($TMP_DIR); $cache = new PrivateCacheStrategy($cacheProvider); $cache->cache($request, $response); $entry = $cache->fetch($request); $this->assertNotNull($entry); $this->assertEquals((string) $response->getBody(), (string) $entry->getResponse()->getBody()); } $this->rrmdir($TMP_DIR); }
/** * {@inheritdoc} */ protected function getCacheObject(RequestInterface $request, ResponseInterface $response) { $cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control')); if ($cacheControl->has('private')) { return; } return parent::getCacheObject($request, $response); }
public function __construct(CacheStorageInterface $cache = null, $ttl) { $this->ttl = $ttl; parent::__construct($cache); }