/** * @param $hostUid * @param $key * @return null|UserText */ public function getTextsByUser($hostUid, $cached = true) { if ($cached) { $pattern = $this->buildKey($hostUid, '*', '*'); $prefix = $this->redis->getOptions()->prefix; $redisKeys = array_map(function ($redisKey) use($prefix) { if (substr($redisKey, 0, strlen($prefix)) == $prefix) { $redisKey = substr($redisKey, strlen($prefix)); } return $redisKey; }, $this->redis->keys($pattern)); if (count($redisKeys) > 0) { $result = array_combine($redisKeys, $this->redis->mget($redisKeys)); foreach ($result as $redisKey => $text) { list(, , $userId, , $key, $lang) = explode(':', $redisKey); $this->texts[$userId][$key][$lang] = $text; } } } if (!$cached || !isset($this->texts[$hostUid]) || !$this->texts[$hostUid]) { $this->texts[$hostUid] = $this->getTextsByHostFromMysql($hostUid); $mset = []; foreach ($this->texts[$hostUid] as $key => $langs) { foreach ($langs as $lang => $text) { $mset[$this->buildKey($hostUid, $key, $lang)] = $text; } } if ($mset) { $this->redis->mset($mset); } } return $this->texts[$hostUid]; }
/** * @param $queueName */ private function getKeys($queueName, $useCache = false) { if (empty($this->keys) || !$useCache) { $this->keys = $this->redis->keys($this->getKeyPrefix($queueName) . '*'); } $prefix = (string) $this->redis->getOptions()->prefix; $this->keys = array_map(function ($key) use($prefix) { return preg_replace('/^' . $prefix . '/', '', $key); }, $this->keys); return $this->keys; }
/** * {@inheritdoc} */ protected function purge() { try { $this->client->transaction(function ($tx) { $keyPrefix = $this->client->getOptions()->prefix; foreach (new \Predis\Collection\Iterator\Keyspace($this->client, '*') as $key) { $tx->del(substr($key, strlen($keyPrefix))); } }); } catch (\Predis\Transaction\AbortedMultiExecException $e) { return false; } return true; }
protected function getParametersAndOptions(Client $client) { $parameters = $client->getConnection()->getParameters(); $options = $client->getOptions(); return array($parameters, $options); }
/** * @return array */ public function findSidsInRedis() { $prefix = (string) $this->redis->getOptions()->prefix; $pattern = $this->getKey(''); $prefixWithPattern = $prefix . $pattern; $keys = $this->redis->keys($pattern . '*'); $sids = array_map(function ($key) use($prefixWithPattern) { return str_replace($prefixWithPattern, '', $key); }, $keys); return $sids; }
/** * @group disconnected */ public function testConstructorWithNullAndNullArguments() { $client = new Client(null, null); $connection = $client->getConnection(); $this->assertInstanceOf('Predis\\Connection\\SingleConnectionInterface', $connection); $parameters = $connection->getParameters(); $this->assertSame($parameters->host, '127.0.0.1'); $this->assertSame($parameters->port, 6379); $options = $client->getOptions(); $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion()); $this->assertFalse($client->isConnected()); }
public function getKeys($pattern = '*') { $prefix = null; if ($this->client->getOptions()->__isset("prefix")) { $prefix = $this->client->getOptions()->__get("prefix")->getPrefix(); } $keys = $this->client->keys($pattern); if ($prefix) { if (is_array($keys)) { for ($i = 0; $i < count($keys); $i++) { $keys[$i] = str_replace($prefix, '', $keys[$i]); } } else { $keys = str_replace($prefix, '', $keys); } } return $keys; }
/** * @group disconnected */ public function testCreateClientWithConnectionFromAggregatedConnection() { $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:')); $this->assertInstanceOf('Predis\\Connection\\ClusterConnectionInterface', $cluster = $client->getConnection()); $this->assertInstanceOf('Predis\\Connection\\SingleConnectionInterface', $node01 = $client->getConnectionById('node01')); $this->assertInstanceOf('Predis\\Connection\\SingleConnectionInterface', $node02 = $client->getConnectionById('node02')); $clientNode02 = $client->getClientFor('node02'); $this->assertInstanceOf('Predis\\Client', $clientNode02); $this->assertSame($node02, $clientNode02->getConnection()); $this->assertSame($client->getOptions(), $clientNode02->getOptions()); }