Пример #1
0
 public function __destruct()
 {
     //XXX 此处假定除factory不会有其他地方new API_Redis
     if ($this->masterClient) {
         $this->masterClient->close();
     }
     if ($this->slaveClient) {
         $this->slaveClient->close();
     }
     if ($this->masterBackClient) {
         $this->masterBackClient->close();
     }
     if ($this->slaveBackClient) {
         $this->slaveBackClient->close();
     }
 }
Пример #2
0
 /**
  * Create a new lock backend with a generated lock id
  *
  * @return Redis_Lock_BackendInterface
  */
 public function createLockBackend()
 {
     if (!$this->getLockBackendClass()) {
         throw new \Exception("Lock backend class does not exist");
     }
     $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_LOCK);
     return $this->backends[] = new $className(Redis_Client::getClient(), Redis_Client::getDefaultPrefix('lock'));
 }
 /**
  * Create a new lock backend with a generated lock id
  *
  * @return Redis_Lock_Backend_Interface
  */
 public function createLockBackend()
 {
     $class = Redis_Client::getClass(Redis_Client::REDIS_IMPL_LOCK);
     if (!class_exists($class)) {
         throw new \Exception("Lock backend class does not exist");
     }
     return $this->backends[] = new $class();
 }
Пример #4
0
 /**
  * Get actual lock backend.
  * 
  * @return Redis_Lock_Backend_Interface
  */
 public static function getBackend()
 {
     if (!isset(self::$instance)) {
         $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_LOCK);
         self::$instance = new $className();
     }
     return self::$instance;
 }
Пример #5
0
 /**
  * Get cache backend
  *
  * @return Redis_Cache_Base
  */
 protected final function getBackend()
 {
     if (null === $this->backend) {
         $class = Redis_Client::getClass(Redis_Client::REDIS_IMPL_CACHE);
         if (null === $class) {
             throw new \Exception("Test skipped due to missing driver");
         }
         $this->backend = new $class('cache');
     }
     return $this->backend;
 }
Пример #6
0
 /**
  * Get cache backend
  *
  * @return Redis_Path_HashLookupInterface
  */
 protected final function getBackend($name = null)
 {
     if (null === $name) {
         // This is needed to avoid conflict between tests, each test
         // seems to use the same Redis namespace and conflicts are
         // possible.
         $name = 'cache' . self::$id++;
     }
     $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_PATH);
     $hashLookup = new $className(Redis_Client::getClient(), 'path', Redis_Client::getDefaultPrefix('path'));
     return $hashLookup;
 }
Пример #7
0
 public function __construct($namespace)
 {
     parent::__construct($namespace);
     $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_CACHE);
     $this->backend = new $className($this->getKey());
     $this->refreshClearMode();
     $this->refreshPermTtl();
     $this->refreshMaxTtl();
 }
Пример #8
0
 function clear($cid = NULL, $wildcard = FALSE)
 {
     $keys = array();
     $skey = $this->getKey(Redis_Cache_Base::TEMP_SET);
     $client = Redis_Client::getClient();
     if (NULL === $cid) {
         switch ($this->getClearMode()) {
             // One and only case of early return.
             case Redis_Cache_Base::FLUSH_NOTHING:
                 return;
                 // Default behavior.
             // Default behavior.
             case Redis_Cache_Base::FLUSH_TEMPORARY:
                 if (Redis_Cache_Base::LIFETIME_INFINITE == variable_get('cache_lifetime', Redis_Cache_Base::LIFETIME_DEFAULT)) {
                     $keys[] = $skey;
                     foreach ($client->smembers($skey) as $tcid) {
                         $keys[] = $this->getKey($tcid);
                     }
                 }
                 break;
                 // Fallback on most secure mode: flush full bin.
             // Fallback on most secure mode: flush full bin.
             default:
             case Redis_Cache_Base::FLUSH_ALL:
                 $keys[] = $skey;
                 $cid = '*';
                 $wildcard = true;
                 break;
         }
     }
     if ('*' !== $cid && $wildcard) {
         // Prefix flush.
         $remoteKeys = $client->keys($this->getKey($cid . '*'));
         // PhpRedis seems to suffer of some bugs.
         if (!empty($remoteKeys) && is_array($remoteKeys)) {
             $keys = array_merge($keys, $remoteKeys);
         }
     } else {
         if ('*' === $cid) {
             // Full bin flush.
             $remoteKeys = $client->keys($this->getKey('*'));
             // PhpRedis seems to suffer of some bugs.
             if (!empty($remoteKeys) && is_array($remoteKeys)) {
                 $keys = array_merge($keys, $remoteKeys);
             }
         } else {
             if (empty($keys) && !empty($cid)) {
                 // Single key drop.
                 $keys[] = $key = $this->getKey($cid);
                 $client->srem($skey, $key);
             }
         }
     }
     if (!empty($keys)) {
         if (count($keys) < Redis_Cache_Base::KEY_THRESHOLD) {
             $client->del($keys);
         } else {
             $pipe = $client->multi(Redis::PIPELINE);
             do {
                 $buffer = array_splice($keys, 0, Redis_Cache_Base::KEY_THRESHOLD);
                 $pipe->del($buffer);
             } while (!empty($keys));
             $pipe->exec();
         }
     }
 }
Пример #9
0
    public function __construct($bin)
    {
        $this->bin = $bin;

        $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_CACHE);
        $this->backend = new $className(Redis_Client::getClient(), $bin, Redis_Client::getDefaultPrefix($bin));

        $this->refreshCapabilities();
        $this->refreshPermTtl();
        $this->refreshMaxTtl();
    }
Пример #10
0
 /**
  * Default contructor
  *
  * Beware that DrupalQueueInterface does not defines the __construct
  * method in the interface yet is being used from DrupalQueue::get() 
  *
  * @param unknown $name
  */
 public function __construct($name)
 {
     $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_QUEUE);
     $this->backend = new $className($name);
 }
Пример #11
0
 public function flushVolatile()
 {
     $client = Redis_Client::getClient();
     $ret = $client->eval(self::EVAL_DELETE_VOLATILE, array($this->getNamespace() . '*'));
     if (1 != $ret) {
         trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
     }
 }
Пример #12
0
 /**
  * Get redis client
  *
  * @return Redis|Predis\Client
  */
 public function getClient()
 {
     // Ugly stateless and static
     return Redis_Client::getClient();
 }
Пример #13
0
 /**
  * Restore client manager
  */
 private final function restoreClientManager()
 {
     Redis_Client::reset();
 }
Пример #14
0
 public function lockReleaseAll($lock_id = NULL)
 {
     if (!isset($lock_id) && empty($this->_locks)) {
         return;
     }
     $client = Redis_Client::getClient();
     $id = isset($lock_id) ? $lock_id : $this->getLockId();
     // We can afford to deal with a slow algorithm here, this should not happen
     // on normal run because we should have removed manually all our locks.
     foreach ($this->_locks as $name => $foo) {
         $key = $this->getKey($name);
         $owner = $client->get($key);
         if (empty($owner) || $owner == $id) {
             $client->del(array($key));
         }
     }
 }
Пример #15
0
 function __construct($bin)
 {
     $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_CACHE);
     $this->backend = new $className($bin);
 }
Пример #16
0
 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);
 }
Пример #17
0
 /**
  * For unit test use only
  */
 static public function reset(Redis_Client_Manager $manager = null)
 {
     self::$manager = $manager;
 }
Пример #18
0
 /**
  * For unit testing only reset internals.
  */
 public static function reset()
 {
     self::$_clientInterface = null;
     self::$_client = null;
 }