You are here

class Redis_Path_Predis in Redis 7.2

Same name and namespace in other branches
  1. 7.3 lib/Redis/Path/Predis.php \Redis_Path_Predis

PhpRedis implementation.

@todo Set high expire value to the hash for rotation when memory is empty React upon cache clear all and rebuild path list?

Hierarchy

Expanded class hierarchy of Redis_Path_Predis

File

lib/Redis/Path/Predis.php, line 10

View source
class Redis_Path_Predis extends Redis_Path_AbstractHashLookup {
  protected function saveInHash($key, $hkey, $hvalue) {
    $client = Redis_Client::getClient();
    $value = $client
      ->hget($key, $hkey);
    if ($value === self::VALUE_NULL) {

      // Remove any null values
      $value = null;
    }
    if ($value) {
      $existing = explode(self::VALUE_SEPARATOR, $value);
      if (!in_array($hvalue, $existing)) {

        // Prepend the most recent path to ensure it always be
        // first fetched one
        // @todo Ensure in case of update that its position does
        // not changes (pid ordering in Drupal core)
        $value = $hvalue . self::VALUE_SEPARATOR . $value;
      }
      else {

        // Do nothing on empty value
        $value = null;
      }
    }
    else {
      if (empty($hvalue)) {
        $value = self::VALUE_NULL;
      }
      else {
        $value = $hvalue;
      }
    }
    if (!empty($value)) {
      $client
        ->hset($key, $hkey, $value);
    }

    // Empty value here means that we already got it
  }
  public function saveAlias($source, $alias, $language = null) {
    if (null === $language) {
      $language = LANGUAGE_NONE;
    }
    if (!empty($source)) {
      $this
        ->saveInHash($this
        ->getKey(self::KEY_ALIAS, $language), $source, $alias);
    }
    if (!empty($alias)) {
      $this
        ->saveInHash($this
        ->getKey(self::KEY_SOURCE, $language), $alias, $source);
    }
  }
  protected function deleteInHash($key, $hkey, $hvalue) {
    $client = Redis_Client::getClient();
    $value = $client
      ->hget($key, $hkey);
    if ($value) {
      $existing = explode(self::VALUE_SEPARATOR, $value);
      if ($index = array_search($hvalue, $existing)) {
        if (1 === count($existing)) {
          $client
            ->hdel($key, $hkey);
        }
        else {
          unset($existing[$index]);
          $client
            ->hset($key, $hkey, implode(self::VALUE_SEPARATOR, $existing));
        }
      }
    }
  }
  public function deleteAlias($source, $alias, $language = null) {
    if (null === $language) {
      $language = LANGUAGE_NONE;
    }
    $this
      ->deleteInHash($this
      ->getKey(self::KEY_ALIAS, $language), $source, $alias);
    $this
      ->deleteInHash($this
      ->getKey(self::KEY_SOURCE, $language), $alias, $source);
  }
  public function deleteLanguage($language) {
    $client = Redis_Client::getClient();
    $client
      ->del($this
      ->getKey(self::KEY_ALIAS, $language));
    $client
      ->del($this
      ->getKey(self::KEY_SOURCE, $language));
  }
  public function lookupInHash($keyPrefix, $hkey, $language = null) {
    $client = Redis_Client::getClient();
    if (null === $language) {
      $language = LANGUAGE_NONE;
      $doNoneLookup = false;
    }
    else {
      if (LANGUAGE_NONE === $language) {
        $doNoneLookup = false;
      }
      else {
        $doNoneLookup = true;
      }
    }
    $ret = $client
      ->hget($this
      ->getKey($keyPrefix, $language), $hkey);
    if ($doNoneLookup && (!$ret || self::VALUE_NULL === $ret)) {
      $previous = $ret;
      $ret = $client
        ->hget($this
        ->getKey($keyPrefix, LANGUAGE_NONE), $hkey);
      if (!$ret && $previous) {

        // Restore null placeholder else we loose conversion to false
        // and drupal_lookup_path() would attempt saving it once again
        $ret = $previous;
      }
    }
    if (self::VALUE_NULL === $ret) {
      return false;

      // Needs conversion
    }
    if (empty($ret)) {
      return null;

      // Value not found
    }
    $existing = explode(self::VALUE_SEPARATOR, $ret);
    return reset($existing);
  }
  public function lookupAlias($source, $language = null) {
    return $this
      ->lookupInHash(self::KEY_ALIAS, $source, $language);
  }
  public function lookupSource($alias, $language = null) {
    return $this
      ->lookupInHash(self::KEY_SOURCE, $alias, $language);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Redis_AbstractBackend::$globalPrefix protected static property
Redis_AbstractBackend::$prefix private property
Redis_AbstractBackend::getClient public function Get redis client
Redis_AbstractBackend::getDefaultPrefix public static function Get global default prefix
Redis_AbstractBackend::getGlobalPrefix public static function Get site default global prefix
Redis_AbstractBackend::getKey public function Get full key name using the set prefix 2
Redis_AbstractBackend::getPrefix final public function Get prefix
Redis_AbstractBackend::KEY_SEPARATOR constant Key components name separator
Redis_AbstractBackend::setPrefix final public function Set prefix
Redis_AbstractBackend::__construct public function Default constructor 2
Redis_Path_HashLookupInterface::KEY_ALIAS constant Alias HASH key prefix
Redis_Path_HashLookupInterface::KEY_SOURCE constant Source HASH key prefix
Redis_Path_HashLookupInterface::VALUE_NULL constant Null value (not existing yet cached value)
Redis_Path_HashLookupInterface::VALUE_SEPARATOR constant Values separator for hash values
Redis_Path_Predis::deleteAlias public function Alias is being deleted for the given source Overrides Redis_Path_HashLookupInterface::deleteAlias
Redis_Path_Predis::deleteInHash protected function
Redis_Path_Predis::deleteLanguage public function A language is being deleted Overrides Redis_Path_HashLookupInterface::deleteLanguage
Redis_Path_Predis::lookupAlias public function Lookup any alias for the given source Overrides Redis_Path_HashLookupInterface::lookupAlias
Redis_Path_Predis::lookupInHash public function
Redis_Path_Predis::lookupSource public function Lookup any source for the given alias Overrides Redis_Path_HashLookupInterface::lookupSource
Redis_Path_Predis::saveAlias public function Alias is being inserted with the given source Overrides Redis_Path_HashLookupInterface::saveAlias
Redis_Path_Predis::saveInHash protected function