Example #1
0
 /**
  * function that takes all keys from source redis, dumps them and imports to the new redis
  */
 public function copy()
 {
     // retrieve all keys from source redis
     $keys = $this->source->keys('*');
     $this->out("Processing %d REDIS keys ...", count($keys), true);
     $hundred = 0;
     $step = 0;
     foreach ($keys as $key) {
         // check for ignored keys and skip the key if it should be ignored
         foreach ($this->ignoredPrefixes as $ignoredPrefix) {
             if (strpos($key, $ignoredPrefix) !== false) {
                 $this->out('.');
                 continue 2;
                 // continue with the next key
             }
         }
         try {
             $ttl = max(0, (int) $this->source->ttl($key));
             $serializedValue = $this->source->dump($key);
             $this->destination->restore($key, $ttl, $serializedValue);
         } catch (Exception $e) {
             $this->out(PHP_EOL . 'ERROR: ' . $key . PHP_EOL);
         }
         if ($step++ % 100 == 0) {
             $this->out(PHP_EOL . $hundred++ . ': ');
         }
         $this->out('o');
     }
     $this->out(PHP_EOL . PHP_EOL);
 }
 /**
  * {@inheritDoc}
  */
 public function get($key, $namespace = KeyValue::DEFAULT_NAMESPACE)
 {
     $value = $this->getValue($key, $namespace);
     $effectiveKey = $this->buildKey($key, $namespace);
     $expiration = $this->redisClient->ttl($effectiveKey);
     $keyValue = new KeyValue($key, $value, $namespace);
     if ($expiration !== -1) {
         $keyValue->setExpirationInSeconds($expiration);
     }
     return $keyValue;
 }
Example #3
0
 /**
  * Refresh the local cache index from the cache.
  * The index is stored in Redis as a set with the key <prefix>::index.
  * Items are added and removed from the index via the write and delete but
  * are also expired automatically by Redis. This function reads the index
  * set and checks the TTL of each key, removing those that no longer exist
  * from the index.
  * @return self
  */
 public function refreshIndex()
 {
     if ($this->indexing) {
         $expired = array();
         foreach ($this->store->sMembers("{$this->prefix}::index") as $k) {
             switch ($this->index[$k] = $this->store->ttl($k)) {
                 case -2:
                     $expired[] = $k;
                     unset($this->index[$k]);
                     break;
                 case -1:
                     // We're not running Redis 2.8 yet so we always get -1
                     // we'll assume all keys have an expiry and remove any without in order to keep the index clean
                     //$this->index[$k] = 2147483648;
                     $expired[] = $k;
                     unset($this->index[$k]);
                     break;
                 default:
                     $this->index[$k] = time() + $this->index[$k];
             }
         }
         foreach ($expired as $k) {
             $this->removeFromIndex($k);
         }
     }
     return $this;
 }
Example #4
0
 public function work()
 {
     foreach ($this->limits as $index => $limit) {
         $counterName = "queue:{$this->tube}:throttle:{$index}";
         $started = (bool) $this->redis->setnx($counterName, $limit['requests']);
         $counter = $this->redis->get($counterName);
         if ($started) {
             $this->redis->transaction(function (MultiExec $transaction) use($counterName, $limit) {
                 $transaction->expire($counterName, $limit['seconds']);
                 $transaction->decr($counterName);
             });
         } else {
             if ($counter > 1) {
                 $this->redis->decr($counterName);
             } else {
                 $this->pheanstalk->pauseTube($this->tube, $this->redis->ttl($counterName));
             }
         }
     }
 }
Example #5
0
 /**
  *
  */
 public function flush()
 {
     $this->client->transaction(function () {
         foreach ($this->buffer as $name => $options) {
             $ttl = $this->client->ttl($name);
             $exists = $this->client->exists($name);
             $this->client->set($name, $options['value']);
             if ($options['expire'] > 0) {
                 $this->client->expire($name, $options['expire']);
             }
             if ($options['expire'] < 0) {
                 if (!$exists || $ttl < 0) {
                     $this->client->expireat($name, time() + $options['expire'] * -1);
                 } else {
                     $this->client->expire($name, $ttl);
                 }
             }
         }
     });
 }