You are here

class PhpRedis in Redis 8

Same name in this branch
  1. 8 src/Lock/PhpRedis.php \Drupal\redis\Lock\PhpRedis
  2. 8 src/Flood/PhpRedis.php \Drupal\redis\Flood\PhpRedis
  3. 8 src/Queue/PhpRedis.php \Drupal\redis\Queue\PhpRedis
  4. 8 src/Client/PhpRedis.php \Drupal\redis\Client\PhpRedis
  5. 8 src/Cache/PhpRedis.php \Drupal\redis\Cache\PhpRedis
  6. 8 src/PersistentLock/PhpRedis.php \Drupal\redis\PersistentLock\PhpRedis

PhpRedis cache backend.

Hierarchy

Expanded class hierarchy of PhpRedis

4 string references to 'PhpRedis'
ClientFactory::getClientInterface in src/ClientFactory.php
Lazy instantiates client proxy depending on the actual configuration.
PhpRedis::getName in src/Client/PhpRedis.php
Get underlying library name used.
RedisCacheTagsChecksum::doInvalidateTags in src/Cache/RedisCacheTagsChecksum.php
Marks cache items with any of the specified tags as invalid.
RedisTestInterfaceTrait::getRedisInterfaceEnv in tests/src/Traits/RedisTestInterfaceTrait.php
Uses an env variable to set the redis client to use for this test.

File

src/Cache/PhpRedis.php, line 12

Namespace

Drupal\redis\Cache
View source
class PhpRedis extends CacheBase {

  /**
   * @var \Redis
   */
  protected $client;

  /**
   * Creates a PHpRedis cache backend.
   *
   * @param $bin
   *   The cache bin for which the object is created.
   * @param \Redis $client
   * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
   * @param \Drupal\redis\Cache\SerializationInterface $serializer
   *   The serialization class to use.
   */
  public function __construct($bin, \Redis $client, CacheTagsChecksumInterface $checksum_provider, SerializationInterface $serializer) {
    parent::__construct($bin, $serializer);
    $this->client = $client;
    $this->checksumProvider = $checksum_provider;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(&$cids, $allow_invalid = FALSE) {

    // Avoid an error when there are no cache ids.
    if (empty($cids)) {
      return [];
    }
    $return = [];

    // Build the list of keys to fetch.
    $keys = array_map([
      $this,
      'getKey',
    ], $cids);

    // Optimize for the common case when only a single cache entry needs to
    // be fetched, no pipeline is needed then.
    if (count($keys) > 1) {
      $pipe = $this->client
        ->multi();
      foreach ($keys as $key) {
        $pipe
          ->hgetall($key);
      }
      $result = $pipe
        ->exec();
    }
    else {
      $result = [
        $this->client
          ->hGetAll(reset($keys)),
      ];
    }

    // Loop over the cid values to ensure numeric indexes.
    foreach (array_values($cids) as $index => $key) {

      // Check if a valid result was returned from Redis.
      if (isset($result[$index]) && is_array($result[$index])) {

        // Check expiration and invalidation and convert into an object.
        $item = $this
          ->expandEntry($result[$index], $allow_invalid);
        if ($item) {
          $return[$item->cid] = $item;
        }
      }
    }

    // Remove fetched cids from the list.
    $cids = array_diff($cids, array_keys($return));
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
    $ttl = $this
      ->getExpiration($expire);
    $key = $this
      ->getKey($cid);

    // If the item is already expired, delete it.
    if ($ttl <= 0) {
      $this
        ->delete($key);
    }

    // Build the cache item and save it as a hash array.
    $entry = $this
      ->createEntryHash($cid, $data, $expire, $tags);
    $pipe = $this->client
      ->multi();
    $pipe
      ->hMset($key, $entry);
    $pipe
      ->expire($key, $ttl);
    $pipe
      ->exec();
  }

  /**
   * {@inheritdoc}
   */
  public function doDeleteMultiple(array $cids) {
    $keys = array_map([
      $this,
      'getKey',
    ], $cids);
    $this->client
      ->del($keys);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
CacheBase::$bin protected property
CacheBase::$checksumProvider protected property The cache tags checksum provider.
CacheBase::$delayedDeletions protected property Delayed deletions for deletions during a transaction.
CacheBase::$lastDeleteAll protected property The last delete timestamp.
CacheBase::$minTtl protected property Minimal TTL to use.
CacheBase::$permTtl protected property Default TTL for CACHE_PERMANENT items.
CacheBase::$serializer protected property The serialization class to use.
CacheBase::createEntryHash protected function Create cache entry.
CacheBase::delete public function Deletes an item from the cache. Overrides CacheBackendInterface::delete
CacheBase::deleteAll public function Deletes all cache items in a bin. Overrides CacheBackendInterface::deleteAll
CacheBase::deleteMultiple public function Deletes multiple items from the cache. Overrides CacheBackendInterface::deleteMultiple
CacheBase::expandEntry protected function Prepares a cached item.
CacheBase::garbageCollection public function Performs garbage collection on a cache bin. Overrides CacheBackendInterface::garbageCollection
CacheBase::get public function Returns data from the persistent cache. Overrides CacheBackendInterface::get
CacheBase::getExpiration protected function Calculate the correct expiration time.
CacheBase::getKey public function Return the key for the given cache key.
CacheBase::getLastDeleteAll protected function Returns the last delete all timestamp.
CacheBase::getPermTtl public function Get TTL for CACHE_PERMANENT items.
CacheBase::getTagForBin protected function Return the key for the tag used to specify the bin of cache-entries.
CacheBase::invalidate public function Marks a cache item as invalid. Overrides CacheBackendInterface::invalidate
CacheBase::invalidateAll public function Marks all cache items as invalid. Overrides CacheBackendInterface::invalidateAll
CacheBase::invalidateMultiple public function Marks cache items as invalid. Overrides CacheBackendInterface::invalidateMultiple
CacheBase::KEY_THRESHOLD constant Computed keys are let's say around 60 characters length due to key prefixing, which makes 1,000 keys DEL command to be something around 50,000 bytes length: this is huge and may not pass into Redis, let's split this off. Some recommend to…
CacheBase::LAST_DELETE_ALL_KEY constant Latest delete all flush KEY name.
CacheBase::LIFETIME_INFINITE constant Temporary cache items lifetime is infinite.
CacheBase::LIFETIME_PERM_DEFAULT constant Default lifetime for permanent items. Approximatively 1 year.
CacheBase::postRootTransactionCommit public function Callback to be invoked after a database transaction gets committed.
CacheBase::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
CacheBase::setMinTtl public function Set the minimum TTL (unit testing only).
CacheBase::setMultiple public function Store multiple items in the persistent cache. Overrides CacheBackendInterface::setMultiple
CacheBase::setPermTtl public function Set the permanent TTL.
PhpRedis::$client protected property Overrides CacheBase::$client
PhpRedis::doDeleteMultiple public function Execute the deletion. Overrides CacheBase::doDeleteMultiple
PhpRedis::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface::getMultiple
PhpRedis::set public function Stores data in the persistent cache. Overrides CacheBackendInterface::set
PhpRedis::__construct public function Creates a PHpRedis cache backend. Overrides CacheBase::__construct
RedisPrefixTrait::$prefix protected property
RedisPrefixTrait::getDefaultPrefix protected function Get global default prefix
RedisPrefixTrait::getPrefix protected function Get prefix
RedisPrefixTrait::setPrefix public function Set prefix