You are here

class Redis_Cache_PhpRedis in Redis 7.3

Same name and namespace in other branches
  1. 7 lib/Redis/Cache/PhpRedis.php \Redis_Cache_PhpRedis
  2. 7.2 lib/Redis/Cache/PhpRedis.php \Redis_Cache_PhpRedis

Predis cache backend.

Hierarchy

Expanded class hierarchy of Redis_Cache_PhpRedis

File

lib/Redis/Cache/PhpRedis.php, line 6

View source
class Redis_Cache_PhpRedis extends Redis_Cache_Base {
  public function setLastFlushTimeFor($time, $volatile = false) {
    $client = $this
      ->getClient();
    $key = $this
      ->getKey(self::LAST_FLUSH_KEY);
    if ($volatile) {
      $client
        ->hset($key, 'volatile', $time);
    }
    else {
      $client
        ->hmset($key, array(
        'permanent' => $time,
        'volatile' => $time,
      ));
    }
  }
  public function getLastFlushTime() {
    $client = $this
      ->getClient();
    $key = $this
      ->getKey(self::LAST_FLUSH_KEY);
    $values = $client
      ->hmget($key, array(
      "permanent",
      "volatile",
    ));
    if (empty($values) || !is_array($values)) {
      $ret = array(
        0,
        0,
      );
    }
    else {
      if (empty($values['permanent'])) {
        $values['permanent'] = 0;
      }
      if (empty($values['volatile'])) {
        $values['volatile'] = 0;
      }
      $ret = array(
        $values['permanent'],
        $values['volatile'],
      );
    }
    return $ret;
  }
  public function get($id) {
    $client = $this
      ->getClient();
    $key = $this
      ->getKey($id);
    $values = $client
      ->hgetall($key);

    // Recent versions of PhpRedis will return the Redis instance
    // instead of an empty array when the HGETALL target key does
    // not exists. I see what you did there.
    if (empty($values) || !is_array($values)) {
      return false;
    }
    return $values;
  }
  public function getMultiple(array $idList) {
    $client = $this
      ->getClient();
    $ret = array();
    $pipe = $client
      ->multi(Redis::PIPELINE);
    foreach ($idList as $id) {
      $pipe
        ->hgetall($this
        ->getKey($id));
    }
    $replies = $pipe
      ->exec();
    foreach (array_values($idList) as $line => $id) {
      if (!empty($replies[$line]) && is_array($replies[$line])) {
        $ret[$id] = $replies[$line];
      }
    }
    return $ret;
  }
  public function set($id, $data, $ttl = null, $volatile = false) {

    // Ensure TTL consistency: if the caller gives us an expiry timestamp
    // in the past the key will expire now and will never be read.
    // Behavior between Predis and PhpRedis seems to change here: when
    // setting a negative expire time, PhpRedis seems to ignore the
    // command and leave the key permanent.
    if (null !== $ttl && $ttl <= 0) {
      return;
    }
    $data['volatile'] = (int) $volatile;
    $client = $this
      ->getClient();
    $key = $this
      ->getKey($id);
    $pipe = $client
      ->multi(Redis::PIPELINE);
    $pipe
      ->hmset($key, $data);
    if (null !== $ttl) {
      $pipe
        ->expire($key, $ttl);
    }
    $pipe
      ->exec();
  }
  public function delete($id) {
    $this
      ->getClient()
      ->del($this
      ->getKey($id));
  }
  public function deleteMultiple(array $idList) {
    $client = $this
      ->getClient();
    $pipe = $client
      ->multi(Redis::PIPELINE);
    foreach ($idList as $id) {
      $pipe
        ->del($this
        ->getKey($id));
    }

    // Don't care if something failed.
    $pipe
      ->exec();
  }
  public function deleteByPrefix($prefix) {
    $client = $this
      ->getClient();
    $ret = $client
      ->eval(self::EVAL_DELETE_PREFIX, array(
      $this
        ->getKey($prefix . '*'),
    ));
    if (1 != $ret) {
      trigger_error(sprintf("EVAL failed: %s", $client
        ->getLastError()), E_USER_ERROR);
    }
  }
  public function flush() {
    $client = $this
      ->getClient();
    $ret = $client
      ->eval(self::EVAL_DELETE_PREFIX, array(
      $this
        ->getKey('*'),
    ));
    if (1 != $ret) {
      trigger_error(sprintf("EVAL failed: %s", $client
        ->getLastError()), E_USER_ERROR);
    }
  }
  public function flushVolatile() {
    $client = $this
      ->getClient();
    $ret = $client
      ->eval(self::EVAL_DELETE_VOLATILE, array(
      $this
        ->getKey('*'),
    ));
    if (1 != $ret) {
      trigger_error(sprintf("EVAL failed: %s", $client
        ->getLastError()), E_USER_ERROR);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Redis_AbstractBackend::$client private property
Redis_AbstractBackend::$namespace private property
Redis_AbstractBackend::$prefix private property
Redis_AbstractBackend::getClient final public function Get client Overrides Redis_BackendInterface::getClient
Redis_AbstractBackend::getKey public function Get prefixed key Overrides Redis_BackendInterface::getKey 1
Redis_AbstractBackend::getNamespace final public function Get namespace Overrides Redis_BackendInterface::getNamespace
Redis_AbstractBackend::getPrefix final public function Get prefix Overrides Redis_BackendInterface::getPrefix
Redis_AbstractBackend::KEY_SEPARATOR constant Key components name separator
Redis_AbstractBackend::setClient final public function Set client Overrides Redis_BackendInterface::setClient
Redis_AbstractBackend::setNamespace final public function Set namespace Overrides Redis_BackendInterface::setNamespace
Redis_AbstractBackend::setPrefix final public function Set prefix Overrides Redis_BackendInterface::setPrefix
Redis_AbstractBackend::__construct public function Default constructor 1
Redis_Cache_Base::EVAL_DELETE_PREFIX constant Delete by prefix lua script
Redis_Cache_Base::EVAL_DELETE_VOLATILE constant Delete volatile by prefix lua script
Redis_Cache_Base::LAST_FLUSH_KEY constant Lastest cache flush KEY name
Redis_Cache_PhpRedis::delete public function
Redis_Cache_PhpRedis::deleteByPrefix public function
Redis_Cache_PhpRedis::deleteMultiple public function
Redis_Cache_PhpRedis::flush public function
Redis_Cache_PhpRedis::flushVolatile public function
Redis_Cache_PhpRedis::get public function
Redis_Cache_PhpRedis::getLastFlushTime public function
Redis_Cache_PhpRedis::getMultiple public function
Redis_Cache_PhpRedis::set public function
Redis_Cache_PhpRedis::setLastFlushTimeFor public function