public function invalidateNode(NodeFormPersistEvent $event) { $node = $event->getNode(); try { if (isset($node->alias)) { $url = $this->requestStack->getMasterRequest()->getSchemeAndHttpHost() . '/' . $node->alias->getAlias(); $this->cacheManager->invalidatePath($url); } $this->cacheManager->invalidateRoute('clastic_front_detail', ['id' => $node->getId()]); } catch (UnsupportedProxyOperationException $e) { // It will expire at some point. } }
public function clearCache(HttpCacheEvent $event) { switch (true) { case $event->getSubject() instanceof ContainerInterface: $this->cacheManager->invalidateRoute('swp_api_templates_list_containers'); $this->cacheManager->invalidateRoute('swp_api_templates_get_container', ['id' => $event->getSubject()->getId()]); break; } try { $this->cacheManager->flush(); } catch (ExceptionCollection $e) { $this->logger->error($e->getMessage()); } }
/** * Process the _tags request attribute, which is set when using the Tag * annotation * * - For a safe (GET or HEAD) request, the tags are set on the response. * - For a non-safe request, the tags will be invalidated. * * @param FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { $request = $event->getRequest(); $response = $event->getResponse(); $tags = array(); // Only set cache tags or invalidate them if response is successful if ($response->isSuccessful()) { $tags = $this->getAnnotationTags($request); } $configuredTags = $this->matchRule($request, $response); if ($configuredTags) { foreach ($configuredTags['tags'] as $tag) { $tags[] = $tag; } foreach ($configuredTags['expressions'] as $expression) { $tags[] = $this->evaluateTag($expression, $request); } } if (!count($tags)) { return; } if ($request->isMethodSafe()) { // For safe requests (GET and HEAD), set cache tags on response $this->cacheManager->tagResponse($response, $tags); } else { // For non-safe methods, invalidate the tags $this->cacheManager->invalidateTags($tags); } }
public function testOnKernelResponsePost() { $tag = new Tag(array('value' => array('item-1', 'item-2'))); $request = new Request(); $request->setMethod('POST'); $request->attributes->set('id', 2); $request->attributes->set('_tag', array($tag)); $event = $this->getEvent($request); $this->cacheManager ->shouldReceive('invalidateTags') ->once() ->with(array('item-1', 'item-2')); $this->listener->onKernelResponse($event); $this->cacheManager ->shouldReceive('invalidateTags') ->once() ->with(array('item-1', 'item-2', 'configured-tag', 'item-2')); $mockMatcher = \Mockery::mock('FOS\HttpCacheBundle\Http\RuleMatcherInterface') ->shouldReceive('matches')->once()->with($request, $event->getResponse())->andReturn(true) ->getMock() ; $this->listener->addRule($mockMatcher, array( 'tags' => array('configured-tag'), 'expressions' => array('"item-" ~ id'), )); $this->listener->onKernelResponse($event); }
/** * Invalidate routes from annotations * * @param array|InvalidateRoute[] $routes * @param Request $request */ private function invalidateRoutes(array $routes, Request $request) { $values = $request->attributes->all(); // if there is an attribute called "request", it needs to be accessed through the request. $values['request'] = $request; foreach ($routes as $route) { $params = array(); if (null !== $route->getParams()) { // Iterate over route params and try to evaluate their values foreach ($route->getParams() as $key => $value) { if (is_array($value)) { $value = $this->getExpressionLanguage()->evaluate($value['expression'], $values); } $params[$key] = $value; } } $this->cacheManager->invalidateRoute($route->getName(), $params); } }
public function purgeAll() { $this->cacheManager->invalidate(array('X-Location-Id' => '.*')); }
public function testTagResponse() { $ban = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\BanInterface'); $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface'); $tags1 = array('post-1', 'posts'); $tags2 = array('post-2'); $tags3 = array('different'); $cacheManager = new CacheManager($ban, $router); $response = new Response(); $response->headers->set($cacheManager->getTagsHeader(), ''); $cacheManager->tagResponse($response, $tags1); $this->assertTrue($response->headers->has($cacheManager->getTagsHeader())); $this->assertEquals(implode(',', $tags1), $response->headers->get($cacheManager->getTagsHeader())); $cacheManager->tagResponse($response, $tags2); $this->assertEquals(implode(',', array_merge($tags1, $tags2)), $response->headers->get($cacheManager->getTagsHeader())); $cacheManager->tagResponse($response, $tags3, true); $this->assertEquals(implode(',', $tags3), $response->headers->get($cacheManager->getTagsHeader())); }