/**
  * Set the cache for a token.
  *
  * @param $key
  * @param string $value
  * @param null $expiration
  */
 protected function cacheSet($key, $value, $expiration = null)
 {
     $this->client->set($key, $value);
     if (!empty($expiration)) {
         $this->ttl = $expiration;
     }
     $this->client->expire($key, $this->ttl);
 }
예제 #2
0
 /**
  * Write value for a key into cache
  *
  * @param string $key Identifier for the data
  * @param mixed $value Data to be cached
  * @return bool True if the data was successfully cached, false on failure
  */
 public function write($key, $value)
 {
     $key = $this->_key($key);
     $value = is_int($value) ? (int) $value : serialize($value);
     if ($this->_config['duration'] === 0) {
         return (string) $this->client->set($key, $value) === 'OK';
     }
     return (string) $this->client->setex($key, $this->_config['duration'], $value) === 'OK';
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, $data, $lifeTime = 0)
 {
     $data = serialize($data);
     if ($lifeTime > 0) {
         $response = $this->client->setex($id, $lifeTime, $data);
     } else {
         $response = $this->client->set($id, $data);
     }
     return $response === true || $response == 'OK';
 }
예제 #4
0
 /**
  * @return array
  */
 public function getPosts() : array
 {
     if ($this->redisClient !== null) {
         $posts = $this->redisClient->get('posts');
         if ($posts) {
             return json_decode($posts, true);
         }
     }
     $blog = $this->getBlogInfo();
     $posts = $this->thumblrClient->getBlogPosts($blog['name'], ['type' => 'text'])->posts;
     $posts = json_encode($posts, JSON_PRETTY_PRINT);
     if ($this->redisClient !== null) {
         $this->redisClient->set('posts', $posts);
         $this->redisClient->expireat('posts', time() + 3600);
     }
     return json_decode($posts, true);
 }
 /**
  * @test
  */
 public function testCacheGet()
 {
     $token = $this->tokenProvider->getAccessTokenByToken('nnch734d00sl2jdk');
     $serializedToken = serialize($token);
     $key = 'tokenProvider/token/key:nnch734d00sl2jdk';
     $this->client->set($key, $serializedToken);
     $cachedToken = $this->tokenProviderCache->cacheGet($key);
     $this->assertEquals($serializedToken, $cachedToken);
 }
예제 #6
0
파일: Service.php 프로젝트: spryker/Storage
 /**
  * @param string $key
  * @param mixed $value
  * @param int|null $ttl
  *
  * @throws \Exception
  *
  * @return mixed
  */
 public function set($key, $value, $ttl = null)
 {
     $key = $this->getKeyName($key);
     if ($ttl === null) {
         $result = $this->client->set($key, $value);
     } else {
         $result = $this->client->setex($key, $ttl, $value);
     }
     $this->addWriteAccessStats($key);
     if (!$result) {
         throw new \Exception('could not set redisKey: "' . $key . '" with value: "' . json_encode($value) . '"');
     }
     return $result;
 }
예제 #7
0
파일: Lock.php 프로젝트: AltThree/Locker
 /**
  * Acquire the lock.
  *
  * Returns true if we were able to acquire the lock, and if we weren't able
  * to acquire it in time, we return false.
  *
  * @return bool
  */
 public function acquire()
 {
     $attempts = 0;
     while (true) {
         $this->token = str_random(32);
         if ($this->redis->set($this->name, $this->token, 'NX', 'PX', $this->timeout)) {
             $this->state = $this->time();
             return true;
         }
         $attempts++;
         if ($attempts >= $this->attempts) {
             return false;
         }
         usleep(random_int($this->min, $this->max));
     }
 }
예제 #8
0
 /**
  * @param SessionInterface $session
  * @param Response $response
  */
 private function postRequestHandle(SessionInterface $session, Response $response)
 {
     if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
         $id = $session->getId();
         $key = 'session:' . $id;
         $content = $session->all();
         unset($content['_token'], $content['flash']);
         $lastSeen = time();
         $content['last_seen'] = $lastSeen;
         $session->set('last_seen', $lastSeen);
         $value = Json::dump($content);
         $this->redis->watch($key);
         $this->redis->multi();
         $this->redis->set($key, $value);
         $this->redis->expire($key, $this->getSessionLifetimeInSeconds());
         $this->redis->exec();
         $cookie = new Cookie($this->key, $id, $this->getCookieExpirationDate(), $config['path'], $config['domain'], Arr::get($config, 'secure', false));
         $response->headers->setCookie($cookie);
     }
 }
예제 #9
0
 /**
  * {@inheritDoc}
  */
 public function set($key, $value)
 {
     return $this->predis->set($key, $value);
 }
예제 #10
0
파일: PredisTest.php 프로젝트: gmo/cache
 /**
  * @group redis-strings
  */
 public function testStringLength()
 {
     $this->assertSame(0, $this->client->strlen('foo'));
     $this->client->set('foo', 'bar');
     $this->assertSame(3, $this->client->strlen('foo'));
 }
예제 #11
0
 /**
  * Save some data to the cache
  *
  * @param $key
  * @param $data
  * @param $timeout
  * @return mixed
  */
 public function save($key, $data, $timeout)
 {
     return $this->redisClient->set($key, $data, 'ex', $timeout);
 }
예제 #12
-1
 /**
  * Sets a cache entry
  *
  * @param $key
  * @param $value
  * @return void
  */
 public function set($key, $value)
 {
     if ($this->ttl === null) {
         $this->client->set($key, $this->serializer->serialize($value));
     } else {
         $this->client->set($key, $this->serializer->serialize($value), 'ex', $this->ttl);
     }
 }