Example #1
1
 /**
  * {@inheritdoc}
  */
 public function save()
 {
     $contents = $this->getForStorage();
     $this->client->set($this->key, $contents);
     if ($this->expire !== null) {
         $this->client->expire($this->key, $this->expire);
     }
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function set($key, $value, $ttl = 0)
 {
     if ($ttl > 0) {
         return $this->predis->setex($key, $ttl, $value);
     }
     return $this->predis->set($key, $value);
 }
Example #3
0
 /**
  * {@inheritDoc}
  */
 public function cache(BatchGeocoded $geocoded)
 {
     $this->redis->set($key = $this->getKey($geocoded->getProviderName(), $geocoded->getQuery()), $this->serialize($geocoded));
     if ($this->expire > 0) {
         $this->redis->expire($key, $this->expire);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function write($sessionId, $data)
 {
     if (0 < $this->ttl) {
         $this->redis->setex($this->getRedisKey($sessionId), $this->ttl, $data);
     } else {
         $this->redis->set($this->getRedisKey($sessionId), $data);
     }
 }
 /**
  * Write a string value to storage
  * @param string $key       the key
  * @param string $value     the string value to be written
  * @param null|int $timeout an optional timout in milliseconds
  * @return string redis result
  */
 public function stringWrite($key, $value, $timeout = null)
 {
     $result = $this->_redis->set($key, $value);
     if ($timeout !== null) {
         $this->_redis->pexpire($key, $timeout);
     }
     return $result;
 }
Example #6
0
 /**
  * @param Password $password
  * @param bool $allowOverwrite
  */
 public function store(Password $password, $allowOverwrite = false)
 {
     if (!$allowOverwrite && $this->get($password->getId())) {
         throw new PhPsstException('The ID already exists', PhPsstException::ID_IS_ALREADY_TAKEN);
     }
     $this->client->set($password->getId(), $password->getJson());
     $this->client->expireat($password->getId(), $password->getTtl());
 }
Example #7
0
 /**
  * @param $key
  * @param $value
  * @throws UnavailableException
  */
 public function add($key, $value)
 {
     // encrypt the tokens before storing in redis
     try {
         $this->client->set($key, $this->encryption->encrypt($value));
     } catch (ServerException $ex) {
         throw new UnavailableException($ex->getMessage());
     }
 }
Example #8
0
 public function getCurrentOrderAction(Application $app)
 {
     // Pause the execution until the display is updated
     while (true === json_decode($this->client->get(BogoBogoSorter::DISPLAYED_KEY))) {
         usleep(500);
     }
     $this->client->set(BogoBogoSorter::DISPLAYED_KEY, json_encode(true));
     return $app->json(json_decode($this->client->get(BogoBogoSorter::DATA_KEY), true));
 }
Example #9
0
 /**
  * * Add a value to the cache under a unique key
  *
  * @param string $key
  * @param mixed  $value
  * @param int    $ttl
  */
 public function set($key, $value, $ttl = null)
 {
     $this->client->set($key, $value);
     if ($ttl) {
         $cmd = $this->client->createCommand('EXPIRE');
         $cmd->setArguments(array($key, $ttl));
         $this->client->executeCommand($cmd);
     }
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, $data, $lifeTime = 0)
 {
     if ($lifeTime > 0) {
         $response = $this->client->setex($id, $lifeTime, $data);
     } else {
         $response = $this->client->set($id, $data);
     }
     return $response === true || $response == 'OK';
 }
Example #11
0
 /**
  * @param Widget $widget
  * @param        $content
  */
 public function save(Widget $widget, $content)
 {
     $hash = $this->getHash($widget);
     if ($hash) {
         $this->redis->set($hash, $content);
         $this->redis->expire($hash, $this->widgetHelper->getCacheTimeout($widget));
         // cache for a week
     }
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, $data, $lifeTime = false)
 {
     if (0 < $lifeTime) {
         $result = $this->redis->setex($id, (int) $lifeTime, serialize($data));
     } else {
         $result = $this->redis->set($id, serialize($data));
     }
     return (bool) $result;
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function save($id, $data, $lifeTime = 0)
 {
     if ($lifeTime > 0) {
         $response = $this->client->setex($this->prefix . $id, $lifeTime, $data);
     } else {
         $response = $this->client->set($this->prefix . $id, $data);
     }
     return $response === true || $response == 'OK';
 }
 /**
  * @return void
  */
 private function checkWriteToStorage()
 {
     try {
         $this->client->set(self::KEY_HEARTBEAT, 'ok');
     } catch (\Exception $e) {
         $this->addDysfunction(self::HEALTH_MESSAGE_UNABLE_TO_WRITE_TO_STORAGE);
         $this->addDysfunction($e->getMessage());
     }
 }
Example #15
0
 /**
  * @param AbstractJob $job
  *
  * @return bool
  * @throws \Cronario\Exception\JobException
  */
 public function save(AbstractJob $job)
 {
     $data = $job->getData();
     if (!$job->isStored()) {
         $job->setId(uniqid());
     }
     $this->redis->set($this->namespace . $job->getId(), json_encode($data));
     return true;
 }
Example #16
0
 /**
  * {@inheritdoc}
  */
 public function set($key, $value, $ttl = null)
 {
     $this->predis->set($key, $this->pack($value));
     if (!$ttl) {
         $ttl = $this->ttl;
     }
     $cmd = $this->predis->createCommand('EXPIRE');
     $cmd->setArguments([$key, $ttl]);
     $this->predis->executeCommand($cmd);
 }
Example #17
0
 /**
  * Stores value by key.
  *
  * @param string  $key    Cache key.
  * @param mixed   $value  Cache value.
  * @param integer $expire Expire time, in seconds(optional).
  *
  * @return boolean
  */
 public function store($key, $value, $expire = 0)
 {
     $value = json_encode($value);
     if ($expire) {
         $this->redisClient->set($key, $value, 'EX', $expire);
     } else {
         $this->redisClient->set($key, $value);
     }
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function set($key, $value, $namespace = KeyValue::DEFAULT_NAMESPACE, $expirationDateTime = KeyValue::NO_EXPIRE)
 {
     $effectiveKey = $this->buildKey($key, $namespace);
     $this->redisClient->set($effectiveKey, serialize($value));
     $keyValue = new KeyValue($key, $value, $namespace, $expirationDateTime);
     if ($expirationDateTime !== KeyValue::NO_EXPIRE) {
         $this->redisClient->expire($effectiveKey, $keyValue->getSecsToExpire());
     }
     return $keyValue;
 }
Example #19
0
 /**
  * Set a value against a key in the cache.
  *
  * @param string   $key     The key to store against
  * @param string   $value   A json_encoded value
  * @param int|null $expires Optional expiry time in seconds from now
  */
 public function set($key, $value = null, $expires = null)
 {
     if ($value === null) {
         return $this->clear($key);
     }
     if ($expires === null) {
         return $this->client->set($key, $value);
     }
     return $this->client->setex($key, $expires, $value);
 }
Example #20
0
 public function write($key, $value, $expires = null)
 {
     $key = $this->prefix . $key;
     $this->store->set($key, serialize($value));
     if (isset($expires)) {
         $expires = $this->makeExpiry($expires);
         $this->store->expire($key, $expires);
     }
     $this->addToIndex($key, $expires);
     return $this;
 }
 public function logout()
 {
     $userId = $this->session->get('userId');
     $newAuthSecret = $this->getRand();
     $oldAuthSecret = $this->redisClient->get("uid:{$userId}:auth");
     $this->redisClient->set("uid:{$userId}:auth", $newAuthSecret);
     $this->redisClient->set("auth:{$newAuthSecret}", $userId);
     $this->redisClient->del("auth:{$oldAuthSecret}");
     $this->session->set('userId', null);
     $this->session->set('username', null);
 }
Example #22
0
 /**
  * Persist an error.
  * @param Error $error
  * @param Handler $handler
  * @return mixed
  */
 public function persist(Error $error, Handler $handler)
 {
     $errorKey = $this->generateRedisKey($error);
     $json = $error->toJson();
     if (is_int($this->ttl)) {
         $this->predisClient->setex($errorKey, $this->ttl, $json);
     } else {
         $this->predisClient->set($errorKey, $json);
     }
     $countKey = $this->generateRedisKey($error, 'count');
     $this->predisClient->incr($countKey);
 }
Example #23
0
 public function set($id, $value, $ttl = null)
 {
     try {
         $this->redis->set($id, $value);
         if ($ttl) {
             $this->redis->expire($id, $ttl);
         }
         return true;
     } catch (Predis\PredisException $e) {
         return false;
     }
 }
Example #24
0
 /**
  * Set a value identified by $key and with an optional $ttl.
  *
  * @param string $key
  * @param mixed  $value
  * @param int    $ttl
  *
  * @return $this
  */
 public function set($key, $value, $ttl = 0)
 {
     $ttl = $this->fromDefaultTtl($ttl);
     if ($ttl >= 0) {
         if ($this->isAvailable()) {
             $this->redis->set($key, $this->storageDataStructure($value));
             if ($ttl > 0) {
                 $this->redis->expire($key, $ttl);
             }
         }
         $this->setChain($key, $value, $ttl);
     }
     return $this;
 }
Example #25
0
 /**
  * Set cache value
  *
  * @param CacheKey $key
  * @param mixed $value
  * @param int $sessionLength
  */
 public function set(CacheKey $key, $value, $sessionLength = null)
 {
     if ($sessionLength == null) {
         $sessionLength = $this->timeThreshold;
     }
     $sKey = $this->getKey($key);
     if (is_array($value) || is_object($value)) {
         $value = serialize($value);
     }
     $this->redis->set($sKey, $value);
     if (!empty($sessionLength) && $sessionLength > 0) {
         $this->redis->expire($sKey, $sessionLength);
     }
 }
Example #26
0
 /**
  * @param $sid
  * @param $session
  */
 public function setHistorySessionToRedis($sid, $session)
 {
     $key = $this->getHistoryKey($sid);
     $this->redis->set($key, serialize($session));
     $this->redis->expire($key, 60 * 60 * 24 * 30);
     // preserve 30 days
 }
Example #27
0
File: Request.php Project: eher/ldd
 public function save($client = null)
 {
     if ($client == null) {
         $client = new PredisClient();
     }
     $client->set($this->getId(), $this->toJson());
 }
Example #28
0
 /**
  * {@inheritdoc}
  */
 public function setCurrentPosition($index)
 {
     $this->initializeRedis();
     $key = $this->getKeyForCurrentPosition();
     $this->redis->set($key, $index);
     $this->currentPosition = $index;
 }
Example #29
0
 /**
  * Creates or modifies keys
  *
  * If $key already exists:
  *
  * - Strings: its value will be overwritten with $value
  * - Other types: $value items will be appended to its value
  *
  * Examples:
  *
  * ``` php
  * <?php
  * // Strings: $value must be a scalar
  * $I->haveInRedis('string', 'Obladi Oblada');
  *
  * // Lists: $value can be a scalar or an array
  * $I->haveInRedis('list', ['riri', 'fifi', 'loulou']);
  *
  * // Sets: $value can be a scalar or an array
  * $I->haveInRedis('set', ['riri', 'fifi', 'loulou']);
  *
  * // ZSets: $value must be an associative array with scores
  * $I->haveInRedis('set', ['riri' => 1, 'fifi' => 2, 'loulou' => 3]);
  *
  * // Hashes: $value must be an associative array
  * $I->haveInRedis('hash', ['obladi' => 'oblada']);
  * ```
  *
  * @param string $type  The type of the key
  * @param string $key   The key name
  * @param mixed  $value The value
  *
  * @throws ModuleException
  */
 public function haveInRedis($type, $key, $value)
 {
     switch (strtolower($type)) {
         case 'string':
             if (!is_scalar($value)) {
                 throw new ModuleException($this, 'If second argument of haveInRedis() method is "string", ' . 'third argument must be a scalar');
             }
             $this->driver->set($key, $value);
             break;
         case 'list':
             $this->driver->rpush($key, $value);
             break;
         case 'set':
             $this->driver->sadd($key, $value);
             break;
         case 'zset':
             if (!is_array($value)) {
                 throw new ModuleException($this, 'If second argument of haveInRedis() method is "zset", ' . 'third argument must be an (associative) array');
             }
             $this->driver->zadd($key, $value);
             break;
         case 'hash':
             if (!is_array($value)) {
                 throw new ModuleException($this, 'If second argument of haveInRedis() method is "hash", ' . 'third argument must be an array');
             }
             $this->driver->hmset($key, $value);
             break;
         default:
             throw new ModuleException($this, "Unknown type \"{$type}\" for key \"{$key}\". Allowed types are " . '"string", "list", "set", "zset", "hash"');
     }
 }
 /**
  * @param string $status
  */
 public function tweet($status)
 {
     $postId = $this->redisClient->incr("global:nextPostId");
     $userId = $this->session->get('userId');
     $post = $userId . "|" . (new \DateTime())->format('d/m/y') . "|" . $status;
     $this->redisClient->set("post:{$postId}", $post);
     $followers = $this->redisClient->smembers("uid:" . $userId . ":followers");
     if ($followers === false) {
         $followers = [];
     }
     $followers[] = $userId;
     foreach ($followers as $fid) {
         $this->redisClient->lpush("uid:{$fid}:posts", [$postId]);
     }
     $this->redisClient->lpush("global:timeline", [$postId]);
     $this->redisClient->ltrim("global:timeline", 0, 1000);
 }