设置缓存有效期

@param string $key 缓存Key
@param int $expire 有效期, 0为不过期
@return bool 成功返回true, 失败false
489211
public function expireAfter($key, $expire = 0)
    {
        $val = $this->mem->get($key);

        if (isset($val)) {
            return $this->set($key, $val, $expire);
        }

        return false;
    }
998888
public static function encrypt($str,$key,$expire=0){
        $expire = sprintf('%010d', $expire ? $expire + time():0);
        $r = md5($key);
        $c = 0;
        $v = "";
        $str    =   $expire.$str;
		$len = strlen($str);
		$l = strlen($r);
        for ($i=0;$i<$len;$i++){
         if ($c== $l) $c=0;
         $v .= substr($r,$c,1) .
             (substr($str,$i,1) ^ substr($r,$c,1));
         $c++;
        }
        return self::ed($v,$key);
    }
265302
public function file_cache($key,$value=null,$expire=70,$prefix=''){
        if (empty($key)) return false;
        $option = array(
            'type'=>'File',
            'prefix'=>$prefix,
            'path'=>CACHE_PATH,
        );
        $expire = empty($expire)?0:$expire;
        $cache = \think\Cache::connect($option);
        if ($value){
            return $cache->set($key,$value,$expire);
        }else{
            return $cache->rm($key);
        }
    }
102144
public function updateExpire(string $key, int $expire): bool
    {
        if (!$this->ifExists($key)) {
            throw new Exception(
                'The key '.$key.' not exist on memcache(d) server',
                self::ERR_KEY_NOT_EXIST
            );
        }
        
        return $this->touch($key, $expire);
    }
506753
public function read(string $key)
    {
        if (isset($this->cache[$key])) {
            $expire = (int)$this->cache[$key]['ttl'];

            if ($expire === 0 || time() < $expire) {
                return $this->cache[$key]['data'];
            }

            $this->delete($key);
            return false;
        }

        return false;
    }
1272823
protected function set($key, $data, $expire) {
		$this->memcache->set($key, $data, null, $expire);
	}
1293617
protected function write(array $keys, $expire = null)
    {
        foreach ($keys as $k => $v) {
            apc_store($k, $v, $expire);
        }

        return true;
    }
3560
protected function doSet(string $srcKey, $data, int $expire = self::EXPIRE): bool
    {
        // 获取文件名与文件位置
        list ($key, $path, $sqlPath) = $this->getFile($srcKey);

        // 创建目录
        makeDir(dirname($path));

        // 保存缓存数据
        write($path, serialize($data), LOCK_EX);

        // 保存缓存的键
        write($sqlPath, $srcKey, LOCK_EX);

        // 取出目录,修改,保存回去
        $fileList = $this->getFileList();
        $fileList[$key] = $expire + time();
        $this->setFileList($fileList);

        return true;
    }
1110920
public function set($key, $value, $expire = 0)
    {
        $this->front->set($key, $value, $expire);
        return $this->back->set($key, $value, $expire);
    }
217956
public function storageLogin($key, $data, $expire = 0)
    {
        $ticket = md5(time() . rand(1000, 9999) . serialize($data));
        $key = $this->getKey($key);
        
        // 设置Cookie
        Cookie::set($key, $ticket, $expire);
        
        // 缓存数据
        Cache::set($ticket, $data, $expire);
    }
224755
public function update($key, $value, $expire = 0)
    {
        $arr = $this->get($key);
        if (!empty($arr)) {
            $arr = array_merge($arr, $value);
            return $this->set($key, $arr, $expire);
        }
        return 0;
    }
700287
public function hSet($hashKey, $key, $data, $expireTime = 3600)
    {
        $data = (is_object($data) || is_array($data)) ? json_encode($data) : $data;

        $data = gzdeflate($data, 0);

        if (strlen($data) > 4096) {
            $data = gzdeflate($data, 6);
        }

        $status = $this->redis->hSet($hashKey, $key, $data);

        $this->redis->expire($hashKey, $expireTime);

        return $status;
    }
1253697
public function add($key, $value, $expiration = null)
    {
        if (null !== $this->logger) {
            $this->logger->startMethod('add', ['key' => $key, 'value' => $value, 'expiration' => $expiration]);
        }

        if (!$this->assertConnected()) {
            $this->setResultFailed(5); // \Memcached::RES_WRITE_FAILURE
            $this->stopMethod();

            return false;
        }

        if (!$this->assertKey($key)) {
            $this->stopMethod();

            return false;
        }

        $key = $this->getPrefix().$key;
        $this->checkForDelayedDelete($key);
        if (!$this->assertHasNotInDeleteQueue($key)) {
            $this->setResultFailed(12); // \Memcached::RES_DATA_EXISTS doc states \Memcached::RES_NOTSTORED :(
            $this->stopMethod();

            return false;
        }

        if (!$this->assertHasNotInCache($key)) {
            $this->setResultFailed(12); // \Memcached::RES_DATA_EXISTS doc states \Memcached::RES_NOTSTORED :(
            $this->stopMethod();

            return false;
        }

        if (!$this->assertValue($value)) {
            $this->stopMethod();

            return false;
        }

        if (!$this->assertExpiration($expiration)) {
            $this->stopMethod();

            return false;
        }

        $this->storeValueInCache($key, $value, $expiration);
        $this->setResultOK();
        $this->stopMethod();

        return true;
    }
911813
public function put($key, $value, $expire = null)
    {
        return call_user_func_array("set{$this->getPrefix()}_transient", [
            $key,
            $value,
            $expire ?: $this->getDefault('expire'),
        ]);
    }
1300883
public function set($key, $value, $expire = 0)
    {
        // 如果expire为0时，设为长期有效。
        if ($expire != 0) {
            $expire = time() + $expire;
        } else {
            $expire = time() * 2;
        }

        $value = '<?php exit;?>' . $expire . serialize($value);

        // 检查目录可写否
        $cachedir = APP_PATH . '/' . Config::get("cache_dir");
        if (!is_writeable($cachedir)) {
            \show_error('ERROR: ' . $cachedir . ' is not writeable!');
        }

        return file_put_contents($this->_filename($key), $value);
    }
391185
public function set($name, $value, $expire = null) {
        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }
        //$key = $name;
        //对数组/对象数据进行缓存处理，保证数据完整性
        $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
        if (is_int($expire) && $expire) {
            $result = $this->handler->setex($name, $expire, $value);
        }
        else {
            $result = $this->handler->set($name, $value);
        }
        return $result;
    }
121917
public function set($key, $value, $expire = 0)
    {
        $value = json_encode($value, JSON_UNESCAPED_UNICODE);
        if ($expire > 0) {
            return $this->hash($key)->setex($key, $expire, $value);
        } else {
            return $this->hash($key)->set($key, $value);
        }
    }