/** * 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); }
/** * 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'; }
/** * {@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'; }
/** * @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); }
/** * @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; }
/** * 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)); } }
/** * @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); } }
/** * {@inheritDoc} */ public function set($key, $value) { return $this->predis->set($key, $value); }
/** * @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')); }
/** * 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); }
/** * 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); } }