public function getTTL($key) { if (!$this->isConnected()) { return false; } return $this->redis->ttl($key); }
/** * @inheritdoc */ public function getMeta($id) { if ($value = $this->get($id)) { return ['expire' => time() + $this->driver->ttl($id), 'data' => $value]; } return false; }
/** * Get cache metadata * * @param string Cache key * @return array */ public function get_metadata($key) { $value = $this->get($key); if ($value !== FALSE) { return array('expire' => time() + $this->_redis->ttl($key), 'data' => $value); } return FALSE; }
/** * Get cache metadata. * * @param string $key Cache key * * @return array */ public function get_metadata($key) { $value = $this->get($key); if ($value !== false) { return ['expire' => time() + $this->_redis->ttl($key), 'data' => $value]; } return false; }
protected function setCached(Bookmark $bookmark, $redisKey) { if ($this->redis !== null) { $this->redis->hSet($redisKey, $bookmark->url, 1); if ($this->redis->ttl($redisKey) < 0) { $this->redis->expire($redisKey, 4 * 7 * 24 * 60 * 60); } } }
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); } }
/** * Get Cache Metadata * * @param string $id Key to get cache metadata on * @param const $scope Cache::LOCAL_SCOPE or Cache::GLOBAL_SCOPE * for local or global scoping of the cache item * @return mixed Cache item metadata */ public function get_metadata($key, $scope = Cache::LOCAL_SCOPE) { $data = $data = unserialize($this->_redis->get($this->unique_key($key, $scope))); $key = $this->unique_key($key, $scope); if (is_array($data)) { list($data, $time) = $data; return array('expire' => ee()->localize->now + $this->_redis->ttl($key), 'mtime' => $time, 'data' => $data); } return FALSE; }
/** * @test Implementation */ public function setOverwritesExistingEntryWithNewUnlimitedLifetime() { $this->setUpBackend(); $this->setUpRedis(); $data = 'data'; $identifier = 'identifier' . uniqid(); $lifetime = 42; $this->backend->set($identifier, $data, array(), $lifetime); $this->backend->set($identifier, $data, array(), 0); $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier); $this->assertSame(31536000, $lifetimeRegisteredInBackend); }
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. }
/** * 获取该k的生存时间,默认返回秒数 * @param bool $inMs 为TRUE时返回ms数(1s=1000ms),为FALSE时,只返回秒数部分(如果有毫秒部分被省略) * @return LONG: The time to live in seconds. If the key has no ttl, -1 will be returned, and -2 if the key doesn't exist. */ public function timeleft($inMs = FALSE) { try { if ($inMs) { return $this->rd->pttl($this->k); } else { return $this->rd->ttl($this->k); } } catch (\RedisException $e) { if ($this->reconnRedis()) { if ($inMs) { return $this->rd->pttl($this->k); } else { return $this->rd->ttl($this->k); } } } return FALSE; }
/** * @param string $key * @return int */ public function ttl($key) { $this->_useCnt++; return $this->_wrapBoolReturn(function () use($key) { return parent::ttl($key); }); }
<?php $redis = new \Redis(); $ok = $redis->connect('127.0.0.1'); $redis->select(9); if (!$ok) { die("redis connect failed"); } $redis->select(9); var_dump($key = $redis->keys("*")[0]); //$redis->setTimeout($key, 1000); var_dump($redis->ttl($key));
* Time: 下午4:32 */ $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $key = 'test-expire-key'; $redis->expire($key, 60); //使用秒为单位 $redis->pExpire($key, 60000); //使用毫秒作为单位 $redis->expireAt($key, 1476868380); //使用Unix timestamp,指定时间过期 $redis->pExpireAt($key, 1476868380000.0); //使用Unix timestamp在指定时间过期,区别是毫秒作为单位 $redis->persist($key); //移除给定key的生存时间 $redis->ttl($key); //返回key剩余的过期时间,使用秒为单位 $redis->pttl($key); //返回key剩余的过期时间,使用毫秒作为单位 $redis->watch($key2); $ret = $redis->multi(Redis::MULTI)->get($key)->incr($key1)->del($key2)->exec(); $pip->incr('a'); $pip->get('test-num2'); $pip->incr('b'); $pip->del('a'); $ret = $pip->exec(); var_dump($ret); exit; $ret = $redis->incr('test-num2')->incr('test-num2')->exec(); exit('aa' . $redis->get('test-num2')); $redis->multi(Redis::PIPELINE);
<?php $redis = new Redis(); $redis->connect('127.0.0.1', '6379'); $redis->select(0); $redis->setex("key", 10, "this is a test"); echo $redis->get("key"); echo "\n"; sleep(6); echo $redis->ttl("key");
/** * 查询某个key的生存时间 * @param $key string 要查询的key名 */ public static function ttl($key) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; $return = $redis->ttl($key); $redis->close(); $redis = null; return $return; }
// If lock is already held, or not held if ($curLockedBy == $lockedBy || !$curLockedBy) { // Set the lock and set the expire timout $redis->hSet($keyName, 'lockedBy', $lockedBy); $redis->expire($keyName, $lockDuration); $return['status'] = 'locked'; // If the lock is renewed, then set to renewed, otherwise set to locked $return['action'] = $curLockedBy == $lockedBy ? 'renewed' : 'locked'; $return['lockedBy'] = $lockedBy; $return['ttl'] = 600; } else { // If the lock is held by another user $return['status'] = 'locked'; $return['action'] = 'none'; $return['lockedBy'] = $curLockedBy; $return['ttl'] = $redis->ttl($keyName); } } else { // If an unlock was requested // If the lock is held by the user requesting the unlock if ($curLockedBy == $lockedBy) { $redis->del($keyName); $return['status'] = 'unlocked'; $return['action'] = 'unlocked'; } else { if (!$curLockedBy) { // If there is no lock $return['status'] = 'unlocked'; $return['action'] = 'none'; } else { // If the lock is held by someone else