master

laravel/framework

Last updated at: 29/12/2023 09:20

TaggedCache.php

TLDR

The TaggedCache.php file in the Illuminate\Cache namespace of the Demo Projects project is a class that extends the base Repository class. It provides methods for storing multiple items in the cache, incrementing and decrementing the value of items, flushing the cache, and getting the tag set instance.

Methods

putMany

This method stores multiple items in the cache for a given number of seconds. If the ttl parameter is null, it calls the putManyForever method to store the items indefinitely.

increment

This method increments the value of an item in the cache.

decrement

This method decrements the value of an item in the cache.

flush

This method removes all items from the cache.

itemKey

This protected method returns the fully qualified key for a tagged item.

taggedItemKey

This method returns a fully qualified key for a tagged item by concatenating the tag's namespace with the item's key.

event

This protected method fires an event for the cache instance.

getTags

This method returns the tag set instance.

Classes

TaggedCache

This class extends the Repository class and provides methods for storing, incrementing, decrementing, and removing items in the cache. It also includes methods for handling tagged items and firing cache events.

<?php

namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\Store;

class TaggedCache extends Repository
{
    use RetrievesMultipleKeys {
        putMany as putManyAlias;
    }

    /**
     * The tag set instance.
     *
     * @var \Illuminate\Cache\TagSet
     */
    protected $tags;

    /**
     * Create a new tagged cache instance.
     *
     * @param  \Illuminate\Contracts\Cache\Store  $store
     * @param  \Illuminate\Cache\TagSet  $tags
     * @return void
     */
    public function __construct(Store $store, TagSet $tags)
    {
        parent::__construct($store);

        $this->tags = $tags;
    }

    /**
     * Store multiple items in the cache for a given number of seconds.
     *
     * @param  array  $values
     * @param  int|null  $ttl
     * @return bool
     */
    public function putMany(array $values, $ttl = null)
    {
        if ($ttl === null) {
            return $this->putManyForever($values);
        }

        return $this->putManyAlias($values, $ttl);
    }

    /**
     * Increment the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function increment($key, $value = 1)
    {
        return $this->store->increment($this->itemKey($key), $value);
    }

    /**
     * Decrement the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function decrement($key, $value = 1)
    {
        return $this->store->decrement($this->itemKey($key), $value);
    }

    /**
     * Remove all items from the cache.
     *
     * @return bool
     */
    public function flush()
    {
        $this->tags->reset();

        return true;
    }

    /**
     * {@inheritdoc}
     */
    protected function itemKey($key)
    {
        return $this->taggedItemKey($key);
    }

    /**
     * Get a fully qualified key for a tagged item.
     *
     * @param  string  $key
     * @return string
     */
    public function taggedItemKey($key)
    {
        return sha1($this->tags->getNamespace()).':'.$key;
    }

    /**
     * Fire an event for this cache instance.
     *
     * @param  \Illuminate\Cache\Events\CacheEvent  $event
     * @return void
     */
    protected function event($event)
    {
        parent::event($event->setTags($this->tags->getNames()));
    }

    /**
     * Get the tag set instance.
     *
     * @return \Illuminate\Cache\TagSet
     */
    public function getTags()
    {
        return $this->tags;
    }
}