示例#1
0
 /**
  * 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';
 }
示例#2
0
 /**
  * {@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';
 }
示例#3
0
文件: PredisTest.php 项目: gmo/cache
 /**
  * @group redis-strings
  */
 public function testSetEx()
 {
     $this->assertEquals('OK', $this->client->setex('foo', 20, 'bar'));
     $this->assertEquals('OK', $this->client->setex('foo', 20, 'baz'));
     $this->assertSame('baz', $this->client->get('foo'));
     $this->assertThat($this->client->ttl('foo'), $this->logicalAnd($this->greaterThan(0), $this->lessThanOrEqual(20)));
 }
示例#4
0
 /**
  * @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;
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function write($sessionId, $data)
 {
     $this->redis->setex($sessionId, $this->ttl, $data);
     return true;
 }