getCacheKey example



        return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName$class$format$context);
    }

    public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
    {
        if (null === $class) {
            return $this->denormalizeFallback($propertyName$class$format$context);
        }

        $cacheKey = $this->getCacheKey($class$context);
        if (!\array_key_exists($cacheKey, self::$denormalizeCache) || !\array_key_exists($propertyName, self::$denormalizeCache[$cacheKey])) {
            self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName$class$context);
        }

        return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName$class$format$context);
    }

    private function getCacheValueForNormalization(string $propertyName, string $class): ?string
    {
        if (!$this->metadataFactory->hasMetadataFor($class)) {
            return null;
        }
$this->locks = [];
    }

    /** * Tries to lock the cache for a given Request, without blocking. * * @return bool|string true if the lock is acquired, the path to the current lock otherwise */
    public function lock(Request $request): bool|string
    {
        $key = $this->getCacheKey($request);

        if (!isset($this->locks[$key])) {
            $path = $this->getPath($key);
            if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
                return $path;
            }
            $h = fopen($path, 'c');
            if (!flock($h, \LOCK_EX | \LOCK_NB)) {
                fclose($h);

                return $path;
            }
$this->locks = [];
    }

    /** * Tries to lock the cache for a given Request, without blocking. * * @return bool|string true if the lock is acquired, the path to the current lock otherwise */
    public function lock(Request $request): bool|string
    {
        $key = $this->getCacheKey($request);

        if (!isset($this->locks[$key])) {
            $path = $this->getPath($key);
            if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
                return $path;
            }
            $h = fopen($path, 'c');
            if (!flock($h, \LOCK_EX | \LOCK_NB)) {
                fclose($h);

                return $path;
            }
public function __construct(
        private readonly SnippetFinder $snippetFinder,
        private readonly AdapterInterface $cache
    ) {
    }

    /** * @return array<string, mixed> */
    public function findSnippets(string $locale): array
    {
        $cacheKey = $this->getCacheKey($locale);
        $item = $this->cache->getItem($cacheKey);

        if ($item->isHit()) {
            return $item->get();
        }

        $snippets = $this->snippetFinder->findSnippets($locale);

        $item->set($snippets);
        $item->tag(self::CACHE_TAG);
        $this->cache->save($item);

        
private array $primaryKeys = [];

    /** * @var array<string, array[]> */
    private array $existences = [];

    private bool $prefetchingCompleted = false;

    public function add(EntityDefinition $definition, array $primaryKey): void
    {
        $key = $this->getCacheKey($primaryKey);
        $this->primaryKeys[$definition->getEntityName()][$key] = $primaryKey;
    }

    public function getPrimaryKeys(): array
    {
        return $this->primaryKeys;
    }

    public function hasExistence(EntityDefinition $definition, array $primaryKey): bool
    {
        $key = $this->getCacheKey($primaryKey);

        
$this->cache = $cache ?? new FileVarExportHandler();
    }

    public function save(string $component): void
    {
        if (! Factories::isUpdated($component)) {
            return;
        }

        $data = Factories::getComponentInstances($component);

        $this->cache->save($this->getCacheKey($component)$data, 3600 * 24);
    }

    private function getCacheKey(string $component): string
    {
        return 'FactoriesCache_' . $component;
    }

    public function load(string $component): bool
    {
        $key = $this->getCacheKey($component);

        
$this->cache = $cache;
        $this->outdatedTokenTtl = $outdatedTokenTtl;
        $this->cacheKeyPrefix = $cacheKeyPrefix;
    }

    public function verifyToken(PersistentTokenInterface $token, #[\SensitiveParameter] string $tokenValue): bool     {
        if (hash_equals($token->getTokenValue()$tokenValue)) {
            return true;
        }

        $cacheKey = $this->getCacheKey($token);
        $item = $this->cache->getItem($cacheKey);
        if (!$item->isHit()) {
            return false;
        }

        $outdatedToken = $item->get();

        return hash_equals($outdatedToken$tokenValue);
    }

    public function updateExistingToken(PersistentTokenInterface $token, #[\SensitiveParameter] string $tokenValue, \DateTimeInterface $lastUsed): void

    public function getTemplateClass(string $name, int $index = null): string
    {
        $key = $this->getLoader()->getCacheKey($name).$this->optionsHash;

        return $this->templateClassPrefix.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $key).(null === $index ? '' : '___'.$index);
    }

    /** * Renders a template. * * @param string|TemplateWrapper $name The template name * * @throws LoaderError When the template cannot be found * @throws SyntaxError When an error occurred during compilation * @throws RuntimeError When an error occurred during rendering */
        $hookName = \str_replace('/', '-', $hook);

        $hook = new StoreApiHook($hookName$request->request->all()$request->query->all()$context);

        $cacheKey = null;
        if ($request->isMethodCacheable()) {
            /** @var StoreApiCacheKeyHook $cacheKeyHook */
            $cacheKeyHook = $hook->getFunction(StoreApiCacheKeyHook::FUNCTION_NAME);

            $this->executor->execute($cacheKeyHook);

            $cacheKey = $cacheKeyHook->getCacheKey();
        }

        $cachedResponse = $this->readFromCache($cacheKey$context$request);

        if ($cachedResponse) {
            return $cachedResponse;
        }

        /** @var StoreApiResponseHook $responseHook */
        $responseHook = $hook->getFunction(StoreApiResponseHook::FUNCTION_NAME);
        // hook: store-api-{hook}
public function testGetSourceContextThrowsOnNotFoundScript(): void
    {
        static::expectException(LoaderError::class);
        $this->scriptLoader->getSourceContext('notExisting');
    }

    public function testGetCacheKey(): void
    {
        static::assertEquals(
            $this->script->getName(),
            $this->scriptLoader->getCacheKey($this->script->getName())
        );
    }

    public function testIsFresh(): void
    {
        $beforeLastModified = $this->script->getLastModified()->sub(new \DateInterval('PT1S'));
        $afterLastModified = $this->script->getLastModified()->add(new \DateInterval('PT1S'));

        static::assertFalse($this->scriptLoader->isFresh($this->script->getName()$beforeLastModified->getTimestamp()));
        static::assertTrue($this->scriptLoader->isFresh($this->script->getName()$afterLastModified->getTimestamp()));

        

        $this->importTemplates();

        static::expectException(LoaderError::class);
        $this->templateLoader->getSourceContext('@StorefrontTheme/storefront/base.html.twig');
    }

    public function testGetCacheKey(): void
    {
        static::assertEquals(
            '@TestTheme/storefront/base.html.twig',
            $this->templateLoader->getCacheKey('@TestTheme/storefront/base.html.twig')
        );
    }

    public function testIsFreshIfTemplateNotFound(): void
    {
        static::assertFalse(
            $this->templateLoader->isFresh('@TestTheme/storefront/base.html.twig', (new \DateTime())->getTimestamp())
        );
    }

    public function testIsFresh(): void
    {
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */)
    {
        return \is_object($data) && !$data instanceof \Traversable;
    }

    /** * @return array|string|int|float|bool|\ArrayObject|null */
    public function normalize(mixed $object, string $format = null, array $context = [])
    {
        if (!isset($context['cache_key'])) {
            $context['cache_key'] = $this->getCacheKey($format$context);
        }

        $this->validateCallbackContext($context);

        if ($this->isCircularReference($object$context)) {
            return $this->handleCircularReference($object$format$context);
        }

        $data = [];
        $stack = [];
        $attributes = $this->getAttributes($object$format$context);
        


        return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName$class$format$context);
    }

    public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
    {
        if (null === $class) {
            return $this->denormalizeFallback($propertyName$class$format$context);
        }

        $cacheKey = $this->getCacheKey($class$context);
        if (!\array_key_exists($cacheKey, self::$denormalizeCache) || !\array_key_exists($propertyName, self::$denormalizeCache[$cacheKey])) {
            self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName$class$context);
        }

        return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName$class$format$context);
    }

    private function getCacheValueForNormalization(string $propertyName, string $class): ?string
    {
        if (!$this->metadataFactory->hasMetadataFor($class)) {
            return null;
        }
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */)
    {
        return \is_object($data) && !$data instanceof \Traversable;
    }

    /** * @return array|string|int|float|bool|\ArrayObject|null */
    public function normalize(mixed $object, string $format = null, array $context = [])
    {
        if (!isset($context['cache_key'])) {
            $context['cache_key'] = $this->getCacheKey($format$context);
        }

        $this->validateCallbackContext($context);

        if ($this->isCircularReference($object$context)) {
            return $this->handleCircularReference($object$format$context);
        }

        $data = [];
        $stack = [];
        $attributes = $this->getAttributes($object$format$context);
        
public function exists($name) {
    // The cache would read in the entire data (instead of only checking whether     // any data exists), and on a potential cache miss, an additional storage     // lookup would have to happen, so check the storage directly.     return $this->storage->exists($name);
  }

  /** * {@inheritdoc} */
  public function read($name) {
    $cache_key = $this->getCacheKey($name);
    if ($cache = $this->cache->get($cache_key)) {
      // The cache contains either the cached configuration data or FALSE       // if the configuration file does not exist.       return $cache->data;
    }
    // Read from the storage on a cache miss and cache the data. Also cache     // information about missing configuration objects.     $data = $this->storage->read($name);
    $this->cache->set($cache_key$data);
    return $data;
  }

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