Exemplo n.º 1
1
 public function set($sKey, $oData, $iTTL = -1)
 {
     $iTTL = $this->buildTTL($iTTL);
     if ($iTTL === 0) {
         return $this->oClient->set($this->buildKey($sKey), $this->serialize($oData));
     }
     return $this->oClient->setex($this->buildKey($sKey), $iTTL, $this->serialize($oData));
 }
Exemplo n.º 2
0
 /**
  * Store data on the cache server.
  *
  * @param string       $key        Key we can use to retrieve the data.
  * @param string|array $data       Data to store on the cache server.
  * @param int          $expiration Time before the data expires on the cache server.
  *
  * @return bool Success/Failure.
  * @access public
  */
 public function set($key, $data, $expiration)
 {
     if ($this->connected === true && $this->ping() === true) {
         return $this->server->set($key, $this->isRedis ? $this->IgBinarySupport ? igbinary_serialize($data) : serialize($data) : $data, $expiration);
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function set($key, $data, $ttl = 0)
 {
     if ($ttl > 0) {
         return $this->redis->setex($key, (int) $ttl, $data);
     }
     return $this->redis->set($key, $data);
 }
 /**
  * {@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);
     }
 }
Exemplo n.º 5
0
 public function set($key, $value, $ttl = 0)
 {
     if ($ttl > 0) {
         return self::$cache->setex($this->getNamespace() . $key, $ttl, json_encode($value));
     } else {
         return self::$cache->set($this->getNamespace() . $key, json_encode($value));
     }
 }
Exemplo n.º 6
0
 /**
  * Save item in the cache.
  * 
  * @param string $key
  * @param string $value
  * @param int $ttl
  * @return void
  * 
  * @throws StorageException if storage error occurs, handler can not be used
  */
 protected function save($key, $value, $ttl)
 {
     try {
         $this->redis->set($key, $value, $ttl);
     } catch (\Exception $e) {
         throw new StorageException("Failed to save redis key: {$key}", 1, $e);
     }
 }
Exemplo n.º 7
0
 /**
  * Set a value in the cache
  * @param string $p_sKey The Key
  * @param mixed $p_mData The Data
  * @param mixed $p_mTTL The Time To Live. Integer or String (strtotime)
  * @return boolean True on succes, False on failure.
  */
 function set($p_sKey, $p_mData, $p_mTTL = null)
 {
     if ($p_mTTL !== null && is_string($p_mData)) {
         $p_mTTL = strtotime($p_mTTL);
     }
     $p_mTTL = $p_mTTL !== null ? $p_mTTL : $this->_defaults['expiry'];
     return $this->_handler->set($p_sKey, $p_mData, $p_mTTL);
 }
Exemplo n.º 8
0
 /**
  * @inheritdoc
  */
 public function put($key, $value, $ttl = 0)
 {
     if ($ttl > 0) {
         return $this->redis->setex($key, $ttl, $value);
     } else {
         return $this->redis->set($key, $value);
     }
 }
Exemplo n.º 9
0
 public function set($key, $value, $ttl = null)
 {
     if (!is_null($ttl)) {
         return $this->connection->set($key, $value, $ttl);
     } else {
         return $this->connection->set($key, $value);
     }
 }
Exemplo n.º 10
0
 public function set($id, $data, $lifetime = false)
 {
     if ($lifetime) {
         return $this->_redis->setex($id, $lifetime, $data);
     } else {
         return $this->_redis->set($id, $data);
     }
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, $data, $lifeTime = 0)
 {
     if (0 === $lifeTime) {
         return $this->_redis->set($id, $data);
     } else {
         return $this->_redis->setex($id, $lifeTime, $data);
     }
 }
Exemplo n.º 12
0
 /**
  * @inheritdoc
  */
 public function store($key, $value, $ttl = null)
 {
     $key = $this->prefix . $key;
     if ($ttl) {
         $this->redis->set($key, serialize($value), $ttl);
         return;
     }
     $this->redis->set($key, serialize($value));
 }
Exemplo n.º 13
0
 /**
  * 设置值(string)会将$value自动转为json格式
  * @param string $key KEY名称
  * @param string|array $value 获取得到的数据
  * @param int $timeOut 时间
  * @return bool
  */
 public function setJson($key, $value, $timeOut = 0)
 {
     $value = json_encode($value);
     $retRes = $this->redis->set($key, $value);
     if ($timeOut > 0) {
         $this->redis->setTimeout($key, $timeOut);
     }
     return $retRes;
 }
Exemplo n.º 14
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;
 }
Exemplo n.º 15
0
 public function testStdClass()
 {
     $data = new \stdClass();
     $data->a = 'a';
     $data->b = 'b';
     $key = new CacheKey('test', rand(1, 1000));
     $this->cache->set($key, $data);
     $val = $this->cache->get($key);
     $this->assertEquals($data, $val);
 }
Exemplo n.º 16
0
 /**
  * Store a value for a given key in the cache.
  *
  * This method does not check if there is a value available for the given key, may cause unexpected behavior if not.
  * Use has() to prevent this issue.
  *
  * @param string $key
  * @param array  $tags
  * @param mixed  $data
  */
 public function set($key, array $tags, $data)
 {
     $key = $this->redis->prefix($key);
     $this->redis->set($key, $this->encoder->encode($data));
     if (is_int($this->expirationTime) && $this->expirationTime > 0) {
         $this->redis->expire($key, $this->expirationTime);
     }
     foreach ($tags as $tag) {
         $this->redis->sadd($this->redis->prefix($tag), $key);
     }
 }
Exemplo n.º 17
0
 public function set($key, $value, $ttl = null)
 {
     if (!$this->isConnected()) {
         return false;
     }
     $status = $this->redis->set($key, $value);
     if ($status === true && $ttl) {
         $this->redis->expire($key, $ttl);
     }
     return $status === true;
 }
Exemplo n.º 18
0
 /**
  * Stores item in Redis.
  *
  * @param \iveeCrest\ICacheable $item to be stored
  *
  * @return boolean true on success
  */
 public function setItem(ICacheable $item)
 {
     $key = md5($this->uniqId . '_' . $item->getKey());
     $ttl = $item->getCacheTTL();
     //emulate memcached behaviour: TTLs over 30 days are interpreted as (absolute) UNIX timestamps
     if ($ttl > 2592000) {
         $this->redis->set($key, serialize($item));
         return $this->redis->expireAt($key, $ttl);
     } else {
         return $this->redis->setex($key, $ttl, serialize($item));
     }
 }
Exemplo n.º 19
0
 /**
  * @param array $data
  * @param array|null $provider
  *
  * @return User
  */
 public function registerUser($data, $provider = null)
 {
     $user = new User(['username' => strtolower($data['_username']), 'email' => strtolower($data['_email']), 'password' => $this->encoder->encodePassword($data['_password'], ''), 'roles' => 'ROLE_USER']);
     if ($this->checkUser($user->getUsername())) {
         throw new BadRequestHttpException();
     }
     $this->redis->hMSet('users:' . $user->getUsername(), $this->toArray($user));
     if (null !== $provider) {
         $user = $this->loadUserByUsername($user->getUsername());
         $this->redis->set('credentials:' . implode(':', $provider), $user->getUsername());
     }
     return $user;
 }
Exemplo n.º 20
0
 /**
  * Store data on the cache server.
  *
  * @param string       $key        Key we can use to retrieve the data.
  * @param string|array $data       Data to store on the cache server.
  * @param int          $expiration Time before the data expires on the cache server.
  *
  * @return bool Success/Failure.
  * @access public
  */
 public function set($key, $data, $expiration)
 {
     if ($this->ping()) {
         switch (NN_CACHE_TYPE) {
             case self::TYPE_REDIS:
             case self::TYPE_MEMCACHED:
                 return $this->server->set($key, $data, $expiration);
             case self::TYPE_APC:
                 return apc_add($key, $data, $expiration);
         }
     }
     return false;
 }
Exemplo n.º 21
0
	/**
	 * 设置缓存
	 * @param string $cache_id
	 * @param mixed $data 缓存数据
	 * @param int $left 缓存时间 秒
	 * @return bool
	 */
	public function set($cache_id, $data, $left = 60) {
		try {
			$value = serialize($data);
			if ($left === 0) {
				$retRes = $this->_redis->set($cache_id, $value);
			} else {
				$retRes = $this->_redis->setex($cache_id, $left, $value);
			}
			return $retRes;
		} catch (Exception $e) {
			$this->_error = $e->getMessage();
			return false;
		}
	}
Exemplo n.º 22
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;
 }
Exemplo n.º 23
0
 public function test($setIterationCount, $getIterationCount)
 {
     $setTime = microtime(true);
     for ($i = 0; $i < $setIterationCount; $i++) {
         $this->redis->set('test' . $i, $this->stringForTest);
     }
     $setTime = microtime(true) - $setTime;
     $getTime = microtime(true);
     for ($i = 0; $i < $getIterationCount; $i++) {
         $this->redis->get('test' . $i % $setIterationCount);
     }
     $getTime = microtime(true) - $getTime;
     return ['set' => $setTime, 'get' => $getTime];
 }
 /**
  * {@inheritDoc}
  */
 public function removeWord($language, $word)
 {
     $word = mb_strtolower($word, mb_detect_encoding($word));
     if ($this->redis->exists($word)) {
         $languages = unserialize($this->redis->get($word));
         if (false !== ($index = array_search($language, $languages))) {
             unset($languages[$index]);
             if (!count($languages)) {
                 $this->redis->del($word);
             } else {
                 $this->redis->set($word, serialize($languages));
             }
         }
     }
     return $this;
 }
Exemplo n.º 25
0
function initModules($application)
{
    $redis = new Redis();
    $redis->connect(CACHE_HOSTNAME, CACHE_PORT);
    $redis->select(CACHE_DB);
    $modulesKey = $application . '-modules';
    $modules = $redis->get($modulesKey);
    if (empty($modules)) {
        $modules = [];
        $dir = __DIR__ . '/../../' . $application . '/modules';
        $dirpath = realpath($dir);
        $filenames = scandir($dir);
        foreach ($filenames as $filename) {
            if ($filename == '.' || $filename == '..') {
                continue;
            }
            if (is_file($dirpath . DIRECTORY_SEPARATOR . $filename . '/Module.php')) {
                $modules[strtolower($filename)] = ['class' => $application . '\\modules\\' . $filename . '\\Module'];
            }
        }
        $redis->set($modulesKey, serialize([$modules, null]));
    } else {
        $modules = unserialize($modules);
        if (!empty($modules[0])) {
            $modules = $modules[0];
        }
    }
    $redis->close();
    return $modules;
}
Exemplo n.º 26
0
 public function testExists()
 {
     $this->redis->delete('key');
     $this->assertFalse($this->redis->exists('key'));
     $this->redis->set('key', 'val');
     $this->assertEquals(True, $this->redis->exists('key'));
 }
Exemplo n.º 27
0
 public function set($key, $data, $group = 'default', $expire = 0)
 {
     if (empty($group)) {
         $group = 'default';
     }
     if (empty($expire) || $expire < 0) {
         $expire = $this->_max_expire;
     }
     if (is_object($data)) {
         $data = clone $data;
     }
     if ($this->_connected && !in_array($group, $this->_no_redis_groups)) {
         if (defined('SAVEQUERIES')) {
             $time = microtime(true);
         }
         try {
             $this->_redis->set($this->_get_redis_key($key, $group), $data, $expire);
         } catch (Exception $e) {
             $this->_connected = false;
         }
         if (defined('SAVEQUERIES')) {
             isset($this->_stats['requests'][$group]['set']) ? $this->_stats['requests'][$group]['set']++ : ($this->_stats['requests'][$group]['set'] = 1);
             isset($this->_stats['requests'][$group]['set_time']) ? $this->_stats['requests'][$group]['set_time'] += microtime(true) - $time : ($this->_stats['requests'][$group]['set_time'] = microtime(true) - $time);
         }
     }
     $this->_cache[$this->_get_prefix($group)][$group][$key] = $data;
     return true;
 }
Exemplo n.º 28
0
 /**
  * Write session data
  *
  * @param   string  $session_id    The session id
  * @param   string  $session_data  The encoded session data
  *
  * @return  boolean  True on success, false otherwise
  *
  * @since   __DEPLOY_VERSION__
  */
 public function write($session_id, $session_data)
 {
     if ($this->ttl > 0) {
         return $this->redis->setex($this->prefix . $session_id, $this->ttl, $session_data);
     }
     return $this->redis->set($this->prefix . $session_id, $session_data);
 }
Exemplo n.º 29
0
 public function testScan()
 {
     if (version_compare($this->version, "2.8.0", "lt")) {
         $this->markTestSkipped();
         return;
     }
     // Key count
     $i_key_count = $this->get_keyspace_count('db0');
     // Have scan retry
     $this->redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
     // Scan them all
     $it = NULL;
     while ($arr_keys = $this->redis->scan($it)) {
         $i_key_count -= count($arr_keys);
     }
     // Should have iterated all keys
     $this->assertEquals(0, $i_key_count);
     // Unique keys, for pattern matching
     $str_uniq = uniqid() . '-' . uniqid();
     for ($i = 0; $i < 10; $i++) {
         $this->redis->set($str_uniq . "::{$i}", "bar::{$i}");
     }
     // Scan just these keys using a pattern match
     $it = NULL;
     while ($arr_keys = $this->redis->scan($it, "*{$str_uniq}*")) {
         $i -= count($arr_keys);
     }
     $this->assertEquals(0, $i);
 }
Exemplo n.º 30
0
 public function testRedisShouldWorkProperly()
 {
     $redis = new \Redis();
     $redis->connect('localhost');
     $this->assertTrue($redis->set('test', 'test'), 'The set method return false');
     $this->assertEquals('test', $redis->get('test'), 'The get method it\'s broken');
 }