/** * {@inheritdoc} */ public function save() { $contents = $this->getForStorage(); $this->client->set($this->key, $contents); if ($this->expire !== null) { $this->client->expire($this->key, $this->expire); } }
/** * {@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); } }
public function createRate($key, $limit, $period) { $this->client->hset($key, 'limit', $limit); $this->client->hset($key, 'calls', 1); $this->client->hset($key, 'reset', time() + $period); $this->client->expire($key, $period); return $this->getRateInfo($key); }
/** * Writes session data. * * @param string $id A session ID * @param string $data A serialized chunk of session data * * @return bool true, if the session was written, otherwise an exception is thrown * * @throws \RuntimeException If the session data cannot be written */ public function write($key, $data) { $result = $this->db->hset($this->getHashKey(), $key, serialize($data)); $expires = (int) $this->options['lifetime']; if ($expires > 0) { $this->db->expire($this->getHashKey(), $expires); } return $result; }
/** * @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 } }
/** * {@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; }
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 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; } }
/** * {@inheirtdoc} * * @return \Orno\Cache\Adapter\RedisAdapter */ public function set($key, $data, $expiry = null) { if (is_null($expiry)) { $expiry = $this->getExpiry(); } if (is_string($expiry)) { $expiry = $this->convertExpiryString($expiry); } $this->redis->set($key, $data); $this->redis->expire($key, $expiry); return $this; }
/** * 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; }
/** * Start the worker and wait for requests */ public function listen() { $context = new \ZMQContext(); $server = new \ZMQSocket($context, \ZMQ::SOCKET_PULL); $server->bind('tcp://127.0.0.1:' . ($this->defaultPort + $this->client->getId() - 1)); $this->logger->info('Client worker ' . $this->client . ' is ready'); while (true) { $request = $server->recv(); $this->logger->debug('Client worker ' . $this->client . ' receiving request : ' . $request); // Check if the input is valid, ignore if wrong $request = json_decode($request, true); if (!$this->isValidInput($request)) { $this->logger->error('Client worker ' . $this->client . ' received an invalid input'); continue; } try { // Call the right method in the client and push to redis the result $result = call_user_func_array(array($this->client, $request['command']), $request['parameters']); } catch (ClientNotReadyException $e) { $this->logger->warning('Client worker ' . $this->client . ' received a request (#' . $request['invokeId'] . ') whereas the client is not ready. This is normal in case of client reconnection process. Ignoring.'); continue; } $key = $this->key . '.client.commands.' . $request['invokeId']; $this->redis->rpush($key, serialize($result)); $this->redis->expire($key, $this->expire); } }
/** * @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 }
/** * Appends data to an existing item on the Redis server. * * @param string $key * @param string $value * @param int $expiration * @return bool */ protected function appendValue($key, $value, $expiration = 0) { if ($this->redis->exists($key)) { $this->redis->append($key, $value); return $this->redis->expire($key, $expiration); } return $this->redis->setex($key, $expiration, $value); }
/** * @param $key * @param $queryName * @param array $options * * @return org_glizy_dataAccess_cache_Iterator */ public function getWithName($key, $queryName, $options = array()) { $data = $this->redis->get($key); if ($data == null) { $data = array(); /** @var org_glizy_dataAccess_cache_Iterator $it */ $it = org_glizy_ObjectFactory::createModelIterator($this->model, $queryName, $options); /** @var org_glizy_dataAccess_cache_ActiveRecord $ar */ foreach ($it as $ar) { $data[] = $ar->getValuesAsArray(); } $this->redis->set($key, $this->serialize($data)); } else { $data = $this->unserialize($data); } if ($this->lifeTime != -1) { $this->redis->expire($key, $this->lifeTime); } return new org_glizy_dataAccess_cache_Iterator($data); }
/** * 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); } }
/** * {@inheritdoc} */ public function save($id, $data, $lifeTime = 0) { $id = $this->_getNamespacedId($id); $data = serialize($data); if ($this->_supportsSetExpire && 0 < $lifeTime) { $result = $this->_redis->setex($id, (int) $lifeTime, $data); } else { $result = $this->_redis->set($id, $data); if ($result && 0 < $lifeTime) { $result = $this->_redis->expire($id, (int) $lifeTime); } } return (bool) $result; }
private function lockSession($sessionId) { $attempts = 1000000 / $this->spinLockWait * $this->lockMaxWait; $this->lockKey = $sessionId . '.lock'; for ($i = 0; $i < $attempts; $i++) { $success = $this->redis->setnx($this->prefix . $this->lockKey, '1'); if ($success) { $this->locked = true; $this->redis->expire($this->prefix . $this->lockKey, $this->lockMaxWait + 1); return true; } usleep($this->spinLockWait); } return false; }
/** * Create nonce for server and salt, expiring after * $Auth_OpenID_SKEW seconds. */ function useNonce($server_url, $timestamp, $salt) { global $Auth_OpenID_SKEW; // save one request to memcache when nonce obviously expired if (abs($timestamp - time()) > $Auth_OpenID_SKEW) { return false; } // SETNX will set the value only of the key doesn't exist yet. $nonceKey = $this->nonceKey($server_url, $salt); $added = $this->redis->setnx($nonceKey, "1"); if ($added) { // Will set expiration $this->redis->expire($nonceKey, $Auth_OpenID_SKEW); return true; } else { return false; } }
/** * @param string $remoteAddress * * @return Session|null */ public function recoverByRemoteAddress($remoteAddress) { $session = null; $oracleData = $this->findSessionInOracleByRemoteAddress($remoteAddress); if (count($oracleData) === 1) { $sid = $oracleData[0]['sid']; $redisKey = $this->buildKey($sid); $data = $this->redis->hgetall($redisKey); if (!$data) { $data['userId'] = (int) $oracleData[0]['userId']; $data['userScreenname'] = $oracleData[0]['userScreenname']; } $this->redis->hmset($redisKey, $data); $this->redis->expire($redisKey, $this->getExpires()); $session = new Session($sid, $this->getRealm(), $data); } return $session; }
/** * */ 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); } } } }); }
/** * @param array $rangeQueries * @param string $key * * @return int */ protected function handleRangeQueries(array $rangeQueries, $key) { $resultKeys = array(); foreach ($rangeQueries as $rangeQuery) { if (!$rangeQuery instanceof ZRangeByScore) { throw new \InvalidArgumentException(sprintf('Range queries must be instances of "Tystr\\RedisOrm\\Query\\ZRangeByScore", "%s" given.', get_class($rangeQuery))); } $resultKey = sprintf('%s:%s', $key, $rangeQuery->getKey()); $this->redis->zinterstore($resultKey, array($rangeQuery->getKey())); $min = $rangeQuery->getMin(); if ($min != '-inf') { $this->redis->zremrangebyscore($resultKey, '-inf', $min); } $max = $rangeQuery->getMax(); if ($max != '+inf') { $this->redis->zremrangebyscore($resultKey, $max, '+inf'); } $this->redis->expire($resultKey, 1200); $resultKeys[] = $resultKey; } return $resultKeys; }
/** * Writes data to the session. * * @param string $id Session id * @param string $data Session data * * @return boolean */ public function write($id, $data) { $this->redis->set($this->prefix . $id, $data); $this->redis->expire($this->prefix . $id, $this->maxLifetime); return true; }
/** * @param string $commandIdentifier * @param mixed $reply */ public function writeCommandReply($commandIdentifier, $reply) { $this->client->rpush(sprintf(self::COMMAND_RESPONSE_KEY, $commandIdentifier), [$reply]); $this->client->expire(sprintf(self::COMMAND_RESPONSE_KEY, $commandIdentifier), $this->timeout); }
/** * {@inheritdoc} */ public function addTemporaryBlockedAccountID(string $user) { $key = $this->buildStorageKeyWithID($user); $this->client->set($key, $key); $this->client->expire($key, 60); }
protected function saveInCache($key, $data) { $value = serialize($data); $this->cache->set($key, $value); $this->cache->expire($key, 3600); }
/** * Check of redis access is OK */ public function check_redis() { $this->line(' * Checking Redis'); $this->table(['Setting', 'Value'], [['Host', config('database.redis.default.host')], ['Port', config('database.redis.default.port')], ['Database', config('database.redis.default.database')]]); $test_key = str_random(64); try { $redis = new Client(['host' => config('database.redis.default.host'), 'port' => config('database.redis.default.port')]); $this->info('Connected to Redis'); $redis->set($test_key, Carbon::now()); $this->info('Set random key of: ' . $test_key); $redis->expire($test_key, 10); $this->info('Set key to expire in 10 sec'); $redis->get($test_key); $this->info('Read key OK'); } catch (Exception $e) { $this->error('Redis test failed. ' . $e->getMessage()); } }
/** * {@inheritdoc} */ public function attachNewApproval($activationKey) { $key = $this->generateRedisKeyByApprovalKey($activationKey); $this->redis->set($key, $key); $this->redis->expire($key, 3600 * 2); }
/** * {@inheritdoc} */ public function write($sessionId, $dataSet) { $this->client->set("wandu.http.sess.{$sessionId}", $dataSet); $this->client->expire("wandu.http.sess.{$sessionId}", $this->expire); }
/** * {@inheritdoc} */ public function addUserId(string $userId) { $key = $this->createRedisStorageKeyByUserId($userId); $this->redis->set($key, $key); $this->redis->expire($key, 60 * 2); }
/** * {@inheritdoc} */ public function write($sessionId, array $dataSet) { $this->client->set("wandu.http.sess.{$sessionId}", serialize($dataSet)); $this->client->expire("wandu.http.sess.{$sessionId}", $this->expire); }