public function set($index, $value) { $this->redis->lset($this->key, $index, $value); if ($this->timeout) { $this->redis->expire($this->key, $this->timeout); } return $this; }
public function incrBy($chiave, $valore = 1, $scadenza = Cache::SCADENZA_DEFAULT) { $x = parent::incrBy($chiave, $valore); if ($scadenza) { parent::expire($chiave, $scadenza); } return $x; }
/** * {@inheritdoc} */ public function acquireLock($lockId) { if ($this->conn->setnx($lockId, 1)) { $this->conn->expire($lockId, $this->lockTtl); return true; } return false; }
/** * 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); } }
public function setTTL($key, $ttl) { if (!$this->isConnected()) { return false; } if (!$this->exists($key)) { return false; } if (!is_numeric($ttl)) { throw new InvalidTtlException($ttl); } return $this->redis->expire($key, (int) $ttl); }
/** * 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; }
/** * @param string $sessionId */ private function lockSession($sessionId) { $attempts = 1000000 / $this->spinLockWait * $this->lockMaxWait; $this->lockKey = $sessionId . '.lock'; for ($i = 0; $i < $attempts; $i++) { $success = $this->redis->setnx($this->prefix . $this->lockKey, '1'); if ($success) { $this->locked = true; $this->redis->expire($this->prefix . $this->lockKey, $this->lockMaxWait + 1); return true; } usleep($this->spinLockWait); } return false; }
/** * @param string $key * @param int $ttl * @return bool */ public function expire($key, $ttl) { $this->_useCnt++; return $this->_wrapBoolReturn(function () use($key, $ttl) { return parent::expire($key, $ttl); }); }
/** * Appends data to an existing item on the Redis server. * * @param string $key * @param string $value * @param int $expiration * @return bool */ protected function appendValue($key, $value, $expiration = 0) { if ($this->redis->exists($key)) { $this->redis->append($key, $value); return $this->redis->expire($key, $expiration); } return $this->redis->setex($key, $expiration, $value); }
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); } } }
/** * Set an item in the cache * * @param string $key * @param mixed $value * @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int) * @return void */ function set($key, $value, $validity = null) { $this->connection->set($key, $value); if ($validity instanceof \DateInterval) { $validity = (new \DateTimeImmutable())->add($validity); } if ($validity instanceof \DateTimeInterface) { $this->connection->expireat($key, $validity->getTimestamp()); } else { if (is_numeric($validity)) { $this->connection->expire($key, $validity); } else { if ($validity != null) { throw new \UnexpectedValueException("Unexpected validity"); } } } }
/** * Set the last modified time to the current time * * @return bool Success status */ public function touch() { $data = $this->cache->get($this->name); if ($data !== false) { $return = $this->cache->set($this->name, $data); if ($this->options['expire']) { return $this->cache->expire($this->name, $this->ttl); } return $return; } return false; }
function test4_set2() { $cache = new Redis(); $cache->pconnect('127.0.0.1', 7001); $t1 = microtime(1); for ($i = 0; $i < 10000; $i++) { $cache->set('qwe' . $i, 'hello' . $i); $cache->expire('qwe' . $i, 1800); } $t2 = microtime(1); var_dump($t2 - $t1); echo "\n"; }
public function testPersist() { $this->redis->set('x', 'y'); $this->redis->expire('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->del('x'); $this->assertTrue(FALSE === $this->redis->persist('x')); // false if the key doesn’t exist. }
/** * Write data to the session. * * @param string $id * @param mixed $data * @return bool */ public function write($id, $data) { // This might be the first set for this session key, so we have to check if it's already exists in redis if ($this->_id !== $id) { // Perform getset to check if session already exists in redis if ($this->_redis->getSet($this->_buildKey($id), $data) === false) { // session doesn't exists in redis, so we have to set expiration $this->_redis->expire($this->_buildKey($id), $this->_config['lifetime']); } // Backup id, so we'll know that expiration has already been set, we'll avoid to perform getset on the next write $this->_id = $id; } else { // This is not the first write in script execution, just update value $this->_redis->set($this->_buildKey($id), $data); } return true; }
/** * 设置一个过期时间 * @param long $expire 要过期的时间,秒或毫秒 * @param bool $isMs 为TRUE时表示$expire是ms(1s=1000ms) */ public function expire($expire, $isMs = FALSE) { try { if ($isMs) { return $this->rd->pexpire($this->k, $expire); } else { return $this->rd->expire($this->k, $expire); } } catch (\RedisException $e) { if ($this->reconnRedis()) { if ($isMs) { return $this->rd->pexpire($this->k, $expire); } else { return $this->rd->expire($this->k, $expire); } } } return FALSE; }
public function _write_cache($output) { $CI =& get_instance(); $path = $CI->config->item('cache_path'); $cache_path = $path === '' ? APPPATH . 'cache/' : $path; $uri = $CI->config->item('base_url') . $CI->config->item('index_page') . $CI->uri->uri_string(); if ($CI->config->item('cache_query_string') && !empty($_SERVER['QUERY_STRING'])) { $uri .= '?' . $_SERVER['QUERY_STRING']; } $cache_path .= md5($uri); $redis = new Redis(); $host = $CI->config->item("redis_host"); $port = $CI->config->item("redis_port"); $redis->connect($host, $port); if (!$redis->ping()) { log_message('error', "Unable to ping to redis {$host}:{$port}"); return false; } if ($this->_compress_output === TRUE) { $output = gzencode($output); if ($this->get_header('content-type') === NULL) { $this->set_content_type($this->mime_type); } } $expire = time() + $this->cache_expiration; $cache_info = serialize(array('last_modified' => time(), 'expire' => $expire, 'headers' => $this->headers)); $output = $cache_info . 'ENDCI--->' . $output; try { $redis->set($cache_path, $output); $redis->expire($cache_path, $this->cache_expiration); $this->set_cache_header($_SERVER['REQUEST_TIME'], $expire); } catch (RedisException $e) { log_message('error', "Unable to set cache key"); return false; } }
public function save(){ Redis::hmset("Source::{$this->id}", 'features', json_encode($this->features), 'type', json_encode($this->type), 'origin', json_encode($this->origin) ); if($this->isUpload()){ Redis::expire("Source::{$this->id}", UPLOAD_EXPIRE_TIME); } }
<?php ob_start(); header("Content-Type:application/json; charset=UTF-8"); include "../ali-api/TopSdk.php"; date_default_timezone_set('Asia/Shanghai'); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); /** * Created by PhpStorm. * User: dong_rui * Date: 2016-01-29 * Time: 14:56 */ session_start(); $ran_num = rand(1000, 9999); $redis->set(session_id() . '_verify_code', $ran_num); $redis->expire(session_id() . '_verify_code', 60); $c = new TopClient(); $c->appkey = '23305908'; $c->secretKey = '9655a0b98c6f8f3bfaf9a40cad1b5696'; $req = new AlibabaAliqinFcSmsNumSendRequest(); $req->setExtend("123456"); $req->setSmsType("normal"); $req->setSmsFreeSignName("百变女人"); $req->setSmsParam("{\"code\":\"{$ran_num}\", \"product\":\"Lady荟\"}"); $req->setRecNum($_POST["phone_number"]); $req->setSmsTemplateCode("SMS_5000839"); $resp = $c->execute($req); $_SESSION["mobile"] = $_POST["phone_number"]; echo json_encode($resp);
/** * set a value expire time * * @param string $key Cache ID * @param int $seconds expire seconds * @return mixed New value on success or FALSE on failure */ public function expire($key, $seconds) { return $this->_redis->expire($key, $seconds); }
/** * @param string $key * @param int $ttl */ public function expire($key, $ttl) { $this->_redis->expire($key, $ttl); }
/** * 设置过期时间 * @param $queue * @param $ttl * @return bool */ public function setExpire($queue, $ttl) { return $this->redis->expire($queue, $ttl); }
/** * 将key->value写入hash表中 * @param $hash string 哈希表名 * @param $data array 要写入的数据 array('key'=>'value') * @param $time longint 过期时间(S) 默认值为0-不设置过期时间 */ public static function hashSet($hash, $data, $time = 0) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; if (is_array($data) && !empty($data)) { $return = $redis->hMset($hash, $data); if ($time && is_numeric($time)) { $redis->expire($hash, $time); } } $redis->close(); $redis = null; return $return; }
public function setRedisData($key, $val) { if (class_exists('Redis')) { try { $redis = new Redis(); $redis->connect($this->getRedisIpPort()); $redis->set($key, $val); $redis->expire($key, $this->getTimeLiveCache()); } catch (RedisException $e) { return false; } return true; } return false; }
if ($redis->exists($keyname)) { $status = $redis->hget($keyname, 'status'); $cookieexist = $redis->hget($keyname, 'cookie'); if ($status == 'processing') { echo "<script>alert('正在为你领取银瓜子~');history.go(-1);</script>"; exit; } elseif ($status == 'processed') { echo "<script>alert('今天的银瓜子已经领完了,明天再来吧~');history.go(-1);</script>"; exit; } elseif ($status == 'problem') { if ($cookieexist == $cookie) { echo "<script>alert('Cookie数据有问题,请尝试重新登录获取后再提交!');history.go(-1);</script>"; exit; } else { echo "<script>alert('成功更新Cookie数据!');</script>"; } } else { echo "<script>alert('你的任务正在队列中,稍后再看看吧~');history.go(-1);</script>"; exit; } } $outtime = strtotime("tomorrow") - time(); if ($outtime <= 3600) { echo "<script>alert('今天太晚了,明天再来吧~');history.go(-1);</script>"; exit; } $redis->hset($keyname, 'cookie', $cookie); $redis->hset($keyname, 'status', 'queuing'); $redis->expire($keyname, $outtime); echo "<script>alert('你的任务已经加入队列中,下一个小时开始领取!');history.go(-1);</script>"; exit;
public function save(){ Redis::hmset("Repository::{$this->id}", 'name', json_encode($this->name), 'url', json_encode($this->url)); // Can't recommend an uploaded file to a user, so don't bother inserting // it into the ranking sets, and go ahead and remove the reference to it // at some point in the near future if($this->isUpload()){ Redis::expire("Repository::{$this->id}", UPLOAD_EXPIRE_TIME); return; } // If it is a repository, we want to insert it into the ranking sets foreach($this->features() as $type => $attributes){ foreach($attributes as $attribute => $value){ Redis::zadd("Match::{$type}::{$attribute}", $value, $this->id); } } }
/** * @inheritdoc */ public function finish($id) { $this->redis->expire($id, $this->expireTime); }
if ($use_cache) { $path = dirname(__FILE__) . trim($_GET['path']); if (file_exists($path)) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->select(10); //断点续传206 http_response_code(206); header('Content-type: image/jpeg'); if ($redis->exists($path)) { echo $redis->get($path); } else { $img = file_get_contents($path); echo $img; $redis->set($path, $img); $redis->expire($path, 86400 * 30); } } else { /* $fileName = date('Y-m-d').'img.log'; $text = $path.PHP_EOF; if($fp = fopen($fileName, 'a')) { if(@fwrite($fp, $text)) { @fclose($fp); } else { @fclose($fp); } } */ } } else {
<?php /** * Created by PhpStorm. * User: xiehaowei * Date: 2016/10/19 * 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');
public function expire($key, $timeout) { return parent::expire($this->generateUniqueKey($key), $timeout); }