/** * @param mixed $value * * @return RedisNoSQLList */ public function prepend($value) { $this->redis->lpush($this->key, $value); if ($this->timeout) { $this->redis->setTimeout($this->key, $this->timeout); } return $this; }
public static function increment($host) { $count = self::$redis->incrBy(self::$prefix . $host, 1); if (self::$redis->get(self::$prefix . $host) == 1) { self::$redis->setTimeout(self::$prefix . $host, self::$ttl); } return $count; }
/** * 设置值(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; }
protected function addValue($key, $value, $expires = 0) { $r = $this->redis->setnx($key, $value); if ($r && $expires) { $this->redis->setTimeout($key, $expires); } return $r; }
public function testttl() { $this->redis->set('x', 'y'); $this->redis->setTimeout('x', 5); for ($i = 5; $i > 0; $i--) { $this->assertEquals($i, $this->redis->ttl('x')); sleep(1); } }
/** * 设置值 * @param string $key KEY名称 * @param string|array $value 获取得到的数据 * @param int $timeOut 时间 * @return bool */ public function set($key, $value, $timeOut = 0) { $value = json_encode($value); $retRes = parent::set($key, $value); if ($timeOut > 0) { parent::setTimeout($key, $timeOut); } return $retRes; }
public function testPersist() { $this->redis->set('x', 'y'); $this->redis->setTimeout('x', 100); $this->assertTrue(TRUE === $this->redis->persist('x')); // true if there is a timeout $this->assertTrue(-1 === $this->redis->ttl('x')); // -1: timeout has been removed. $this->assertTrue(FALSE === $this->redis->persist('x')); // false if there is no timeout $this->redis->delete('x'); $this->assertTrue(FALSE === $this->redis->persist('x')); // false if the key doesn’t exist. }
/** * Add to the cache * * Add a new variable to the cache that you will then be able * to retrieve using the $this->get($name) method. * * @param string $name The name of the cache variable to store. * @param string $value The value of the cache variable to store. * @param integer $expire When should it expire? Default: 900 seconds. * * @return boolean Depending on the success of the operation, * either true or false. */ public function add($name, $value, $expiry = 900) { $this->redis->add($name, serialize($value)); $this->redis->setTimeout($name, $expiry); }