invalidate example

public function testRestoresResponseContentFromEntityStoreWithLookup()
    {
        $this->storeSimpleEntry();
        $response = $this->store->lookup($this->request);
        $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test'))$response->headers->get('X-Body-File'));
    }

    public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
    {
        $this->storeSimpleEntry();
        $this->store->invalidate($this->request);
        $response = $this->store->lookup($this->request);
        $this->assertInstanceOf(Response::class$response);
        $this->assertFalse($response->isFresh());
    }

    public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
    {
        $req = Request::create('/test');
        $this->store->invalidate($req);
        $this->assertNull($this->store->lookup($this->request));
    }

    

        // Delete the payload and invalid flag of all rules         $update = new RetryableQuery(
            $this->connection,
            $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
        );
        $update->execute();
    }

    public function onRuleWritten(): void
    {
        $this->cartRuleLoader->invalidate();
    }
}
/** * `invalidate()` allows you to invalidate all cache entries with the given tag. * * @param array $tags The tags for which all cache entries should be invalidated as array. * * @example cache-invalidation/simple-script.twig Invalidate a hard coded tag. * @example cache-invalidation/filter-by-entity.twig Build tags based on written entities and invalidate those tags. * @example cache-invalidation/complex-invalidation.twig Build tags if products with a specific property is created and invalidate those tags. */
    public function invalidate(array $tags): void
    {
        $this->cacheInvalidator->invalidate($tags);
    }
}
/** * @param string[] $tags * * @dataProvider providerTags */
    public function testInvalidate(array $tags, string $prefix = ''): void
    {
        $this->mockHandler->append(new GuzzleResponse(200, []));

        $gateway = new FastlyReverseProxyGateway($this->client, 'test', 'key', '0', 3, $prefix, '', 'http://localhost');
        $gateway->invalidate($tags);
        $gateway->flush();

        $lastRequest = $this->mockHandler->getLastRequest();
        static::assertNotNull($lastRequest);
        static::assertSame('/service/test/purge', $lastRequest->getRequestTarget());
        static::assertSame([$prefix . 'foo']$lastRequest->getHeader('surrogate-key'));
        static::assertSame(['key']$lastRequest->getHeader('Fastly-Key'));

        static::assertCount(0, $this->mockHandler);
    }

    
$this->cacheCreated = $cache->created;
      $this->storage = $cache->data;
    }
  }

  /** * Invalidate the cache. */
  protected function invalidateCache() {
    // Invalidate the cache to make sure that other requests immediately see the     // deletion before this request is terminated.     $this->cache->invalidate($this->getCid());
    $this->cacheInvalidated = TRUE;
  }

}
return;
        }

        $container = Shopware()->Container();
        if (!$container->initialized('session')) {
            return;
        }

        /** @var Shopware_Plugins_Frontend_Statistics_Bootstrap $plugin */
        $plugin = Shopware()->Plugins()->Frontend()->Statistics();
        if ($plugin->checkIsBot($args->getRequest()->getHeader('USER_AGENT') ?: '')) {
            $this->get('session')->invalidate();
        }
    }

    /** * Event listener method * * @return \sSystem */
    public function onInitResourceSystem(Enlight_Event_EventArgs $args)
    {
        $config = Shopware()->Config();

        


            $ruleIds = $this->hydrateRuleIds($this->getForeignKeyFields($definition)$nestedEvent$ruleIds);
        }

        if (empty($ruleIds)) {
            return;
        }

        $this->update(Uuid::fromBytesToHexList(array_unique(array_filter($ruleIds))));

        $this->cacheInvalidator->invalidate([CachedRuleLoader::CACHE_KEY]);
    }

    /** * @param list<string> $ids */
    public function update(array $ids): void
    {
        $associationFields = $this->getAssociationFields();

        $areas = $this->getAreas($ids$associationFields);

        
if ($item = $this->getByHash($cidhash)) {
      $item->expire = REQUEST_TIME - 1;
      $this->writeItem($cidhash$item);
    }
  }

  /** * {@inheritdoc} */
  public function invalidateMultiple(array $cids) {
    foreach ($cids as $cid) {
      $this->invalidate($cid);
    }
  }

  /** * {@inheritdoc} */
  public function invalidateAll() {
    foreach ($this->storage()->listAll() as $cidhash) {
      $this->invalidateByHash($cidhash);
    }
  }

  
$gateway->tag(['tag-1', 'tag-2'], 'https://example.com', $response);

        static::assertSame('tag-1 tag-2', $response->headers->get('xkey'));
    }

    public function testInvalidate(): void
    {
        $gateway = new VarnishReverseProxyGateway(['http://localhost'], 0, $this->client);

        $this->mockHandler->append(new GuzzleResponse(200, [], ''));

        $gateway->invalidate(['tag-1', 'tag-2']);

        $request = $this->mockHandler->getLastRequest();
        static::assertNotNull($request);

        static::assertEquals('PURGE', $request->getMethod());
        static::assertEquals('http://localhost', $request->getUri()->__toString());
        static::assertEquals('tag-1 tag-2', $request->getHeader('xkey')[0]);
    }

    /** * @dataProvider providerExceptions */


        $event->setResponse($response);
    }

    public function onSystemConfigChanged(SystemConfigChangedEvent $event): void
    {
        if ($event->getKey() !== self::SYSTEM_CONFIG_KEY) {
            return;
        }

        $this->cacheInvalidator->invalidate([self::ALL_TAG]);
    }

    private static function buildName(string $salesChannelId, string $domainId, string $languageId): string
    {
        return 'error-page-' . $salesChannelId . $domainId . $languageId;
    }

    private function generateKey(string $salesChannelId, string $domainId, string $languageId, Request $request, SalesChannelContext $context): string
    {
        $key = self::buildName($salesChannelId$domainId$languageId) . md5($this->generator->getSalesChannelContextHash($context));

        

#[Package('core')] class FinishController extends AbstractController
{
    #[Route('/finish', name: 'finish', defaults: ['step' => 3])]     public function default(Request $request, #[Autowire('%kernel.cache_dir%')] string $cacheDir): Response     {
        // @codeCoverageIgnoreStart         if ($request->getMethod() === Request::METHOD_POST) {
            if ($request->hasSession()) {
                $request->getSession()->invalidate();
            }

            $self = $_SERVER['SCRIPT_FILENAME'];
            \assert(\is_string($self));

            $redirectUrl = $request->getBasePath() . '/admin';

            // Cleanup our generated cache dir in system temporary directory             $fs = new Filesystem();
            $fs->remove($cacheDir);

            
static::assertArrayHasKey('store-api-cache-script::response', $traces);
        // assert that the response was cached, and thus the script was not called again         static::assertCount(1, $traces['store-api-cache-script::response']);

        static::assertArrayHasKey('apiAlias', $response);
        static::assertArrayHasKey('foo', $response);
        static::assertEquals('bar', $response['foo']);
        static::assertSame('store_api_cache_script_response', $response['apiAlias']);

        // invalidate the custom cache tag         $cacheInvalidator = $this->getContainer()->get(CacheInvalidator::class);
        $cacheInvalidator->invalidate(['my-custom-tag'], true);

        $this->browser->request('GET', '/store-api/script/cache-script');
        static::assertNotFalse($this->browser->getResponse()->getContent());
        $response = \json_decode($this->browser->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        static::assertSame(Response::HTTP_OK, $this->browser->getResponse()->getStatusCode()$this->browser->getResponse()->getContent());

        $traces = $this->getScriptTraces();
        static::assertArrayHasKey('store-api-cache-script::response', $traces);
        // assert that when the cache tag was invalidated the script is executed again         static::assertCount(2, $traces['store-api-cache-script::response']);

        
static::assertSame(['product-1', 'product-2']$parameters);
    }

    public function testInvalidate(): void
    {
        $this->redis->expects(static::once())->method('eval')->willReturn(['/foo']);
        $this->redis->expects(static::once())->method('del')->with('product-1');

        $this->mockHandler->append(new Response(200, [], null));

        $this->gateway->invalidate(['product-1']);

        static::assertNotNull($this->mockHandler->getLastRequest());
        static::assertSame('http://localhost/foo', $this->mockHandler->getLastRequest()->getUri()->__toString());
    }

    public function testInvalidateFails(): void
    {
        $this->redis->expects(static::once())->method('eval')->willReturn(['/foo']);

        $this->mockHandler->append(new Response(500, [], null));

        
public function cacheableAction()
    {
        $response = new Response('all good');
        $response->setSharedMaxAge(100);

        return $response;
    }

    public function logoutAction(Request $request)
    {
        $request->getSession()->invalidate();

        return new Response('<html><body>Session cleared.</body></html>');
    }

    public function setFlashAction(Request $request$message)
    {
        $session = $request->getSession();
        $session->getFlashBag()->set('notice', $message);

        return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
    }

    
private readonly array $states
    ) {
    }

    public function __destruct()
    {
        $this->gateway->flush();
    }

    public function __invoke(InvalidateCacheEvent $event): void
    {
        $this->gateway->invalidate($event->getKeys());
    }

    public function lookup(Request $request): ?Response
    {
        return null;
    }

    public function write(Request $request, Response $response): string
    {
        $tags = $this->tracer->get('all');

        
Home | Imprint | This part of the site doesn't use cookies.