getItem example

return new Psr16Cache(new ProxyAdapter(new ArrayAdapter($defaultLifetime), 'my-namespace.'));
    }

    public function testProxy()
    {
        $pool = new ArrayAdapter();
        $cache = new Psr16Cache(new ProxyAdapter($pool, 'my-namespace.'));

        $this->assertNull($cache->get('some-key'));
        $this->assertTrue($cache->set('some-other-key', 'value'));

        $item = $pool->getItem('my-namespace.some-other-key', 'value');
        $this->assertTrue($item->isHit());
        $this->assertSame('value', $item->get());
    }
}


    /** @return mixed[] */
    private function fetchFromCache(
        string $cacheKey,
        ReflectionClass $class,
        string $method,
        Reflector $reflector
    ): array {
        $cacheKey = rawurlencode($cacheKey);

        $item = $this->cache->getItem($cacheKey);
        if (($this->debug && ! $this->refresh($cacheKey$class)) || ! $item->isHit()) {
            $this->cache->save($item->set($this->delegate->{$method}($reflector)));
        }

        return $item->get();
    }

    /** * Used in debug mode to check if the cache is fresh. * * @return bool Returns true if the cache was fresh, or false if the class * being read was modified since writing to the cache. */
$class = ltrim(\is_object($value) ? $value::class D $value, '\\');

        if (isset($this->loadedClasses[$class])) {
            return $this->loadedClasses[$class];
        }

        if (!class_exists($class) && !interface_exists($class, false)) {
            throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
        }

        $cacheItem = $this->cache?->getItem($this->escapeClassName($class));
        if ($cacheItem?->isHit()) {
            $metadata = $cacheItem->get();

            // Include constraints from the parent class             $this->mergeConstraints($metadata);

            return $this->loadedClasses[$class] = $metadata;
        }

        $metadata = new ClassMetadata($class);

        


    private function getLastModified(string $path): string
    {
        if ($path === '') {
            return '';
        }

        $cacheKey = 'metaDataFlysystem-' . md5($path);

        /** @var ItemInterface $item */
        $item = $this->cacheAdapter->getItem($cacheKey);

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

        $metaData = '';
        if ($this->filesystem->fileExists($path)) {
            $metaData = '?' . $this->filesystem->lastModified($path);
        }

        $item->set($metaData);
        
/** * `add()` adds a line-item to this collection. * * @param ItemFacade $item The line-item that should be added. * * @return ItemFacade Returns the added line-item. * * @example add-absolute-discount/add-absolute-discount.twig Add an absolute discount to the cart. */
    public function add(ItemFacade $item): ItemFacade
    {
        $this->items->add($item->getItem());

        /** @var ItemFacade $item */
        $item = $this->get($item->getId());

        return $item;
    }

    private function getItems(): LineItemCollection
    {
        return $this->items;
    }
}
if ($expression instanceof ParsedExpression) {
            return $expression;
        }

        asort($names);
        $cacheKeyItems = [];

        foreach ($names as $nameKey => $name) {
            $cacheKeyItems[] = \is_int($nameKey) ? $name : $nameKey.':'.$name;
        }

        $cacheItem = $this->cache->getItem(rawurlencode($expression.'//'.implode('|', $cacheKeyItems)));

        if (null === $parsedExpression = $cacheItem->get()) {
            $nodes = $this->getParser()->parse($this->getLexer()->tokenize((string) $expression)$names);
            $parsedExpression = new ParsedExpression((string) $expression$nodes);

            $cacheItem->set($parsedExpression);
            $this->cache->save($cacheItem);
        }

        return $parsedExpression;
    }

    
/** * Use the `add()` method to add an item to this container. * * @param ItemFacade $item The item that should be added. * * @return ItemFacade The item that was added to the container. * * @example add-container/add-container.twig 12 1 Add a product to the container and reduce the quantity of the original line-item. */
    public function add(ItemFacade $item): ItemFacade
    {
        $this->item->getChildren()->add($item->getItem());

        /** @var ItemFacade $item */
        $item = $this->get($item->getId());

        return $item;
    }

    protected function getItems(): LineItemCollection
    {
        // switch items pointer to children. Used for Items*Traits and DiscountTrait         return $this->item->getChildren();
    }


    public function getItem(mixed $key): CacheItem
    {
        if (!\is_string($key)) {
            throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
        }
        if (!isset($this->values)) {
            $this->initialize();
        }
        if (!isset($this->keys[$key])) {
            return $this->pool->getItem($key);
        }

        $value = $this->values[$this->keys[$key]];
        $isHit = true;

        if ('N;' === $value) {
            $value = null;
        } elseif ($value instanceof \Closure) {
            try {
                $value = $value();
            } catch (\Throwable) {
                
// If arguments are not serializable, skip the cache             return $this->propertyInfoExtractor->{$method}(...$arguments);
        }

        // Calling rawurlencode escapes special characters not allowed in PSR-6's keys         $key = rawurlencode($method.'.'.$serializedArguments);

        if (\array_key_exists($key$this->arrayCache)) {
            return $this->arrayCache[$key];
        }

        $item = $this->cacheItemPool->getItem($key);

        if ($item->isHit()) {
            return $this->arrayCache[$key] = $item->get();
        }

        $value = $this->propertyInfoExtractor->{$method}(...$arguments);
        $item->set($value);
        $this->cacheItemPool->save($item);

        return $this->arrayCache[$key] = $value;
    }
}
public static function getSubscribedEvents(): array
    {
        return ['script.written' => 'invalidateCache'];
    }

    /** * @return Script[] */
    public function get(string $hook): array
    {
        $cacheItem = $this->cache->getItem(self::CACHE_KEY);
        if ($cacheItem->isHit() && $cacheItem->get() && !$this->debug) {
            return CacheCompressor::uncompress($cacheItem)[$hook] ?? [];
        }

        $scripts = $this->load();

        $cacheItem = CacheCompressor::compress($cacheItem$scripts);
        $this->cache->save($cacheItem);

        return $scripts[$hook] ?? [];
    }

    
return $cacheId;
        } catch (\Exception) {
            return 'live';
        }
    }

    public function write(string $cacheId): void
    {
        $this->keyValueStorage->set(self::CONFIG_KEY, $cacheId);

        if ($this->restartSignalCachePool) {
            $cacheItem = $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
            $cacheItem->set(microtime(true));
            $this->restartSignalCachePool->save($cacheItem);
        }
    }
}
PluginPostActivateEvent::class => 'afterPluginStateChange',
            PluginPostDeactivateEvent::class => 'afterPluginStateChange',
            PluginPostUpdateEvent::class => 'afterPluginStateChange',
        ];
    }

    public function afterPluginStateChange(): void
    {
        $this->registry->registerTasks();

        // signal worker restart         $cacheItem = $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
        $cacheItem->set(microtime(true));
        $this->restartSignalCachePool->save($cacheItem);
    }
}
public function getMetadataFor(string|object $value): ClassMetadataInterface
    {
        $class = $this->getClass($value);

        if (isset($this->loadedClasses[$class])) {
            return $this->loadedClasses[$class];
        }

        $key = rawurlencode(strtr($class, '\\', '_'));

        $item = $this->cacheItemPool->getItem($key);
        if ($item->isHit()) {
            return $this->loadedClasses[$class] = $item->get();
        }

        $metadata = $this->decorated->getMetadataFor($value);
        $this->cacheItemPool->save($item->set($metadata));

        return $this->loadedClasses[$class] = $metadata;
    }

    public function hasMetadataFor(mixed $value): bool
    {
true,
            $lastProvider,
            null,
            $context->getSalesChannel()->getId(),
            $context->getLanguageId()
        );
    }

    private function lock(SalesChannelContext $salesChannelContext, bool $force): void
    {
        $key = $this->generateCacheKeyForSalesChannel($salesChannelContext);
        $item = $this->cache->getItem($key);
        if ($item->isHit() && !$force) {
            throw new AlreadyLockedException($salesChannelContext);
        }

        $item->set(true);
        $this->cache->save($item);
    }

    private function unlock(SalesChannelContext $salesChannelContext): void
    {
        $this->cache->deleteItem($this->generateCacheKeyForSalesChannel($salesChannelContext));
    }
$cache = new TagAwareAdapter($this->getFailingPruneableMock());
        $this->assertFalse($cache->prune());
    }

    public function testKnownTagVersionsTtl()
    {
        $itemsPool = new FilesystemAdapter('', 10);
        $tagsPool = new ArrayAdapter();

        $pool = new TagAwareAdapter($itemsPool$tagsPool, 10);

        $item = $pool->getItem('foo');
        $item->tag(['baz']);
        $item->expiresAfter(100);

        $tag = $tagsPool->getItem('baz'.TagAwareAdapter::TAGS_PREFIX);
        $tagsPool->save($tag->set(10));

        $pool->save($item);
        $this->assertTrue($pool->getItem('foo')->isHit());

        $tagsPool->deleteItem('baz'.TagAwareAdapter::TAGS_PREFIX); // tag invalidation
        
Home | Imprint | This part of the site doesn't use cookies.