/** * 读取或者设置缓存 * @access public * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param mixed $expire 缓存有效期 * @return mixed */ public function cache($key, $expire = null) { if ($this->isGet()) { if (false !== strpos($key, ':')) { $param = $this->param(); foreach ($param as $item => $val) { if (is_string($val) && false !== strpos($key, ':' . $item)) { $key = str_replace(':' . $item, $val, $key); } } } elseif ('__URL__' == $key) { // 当前URL地址作为缓存标识 $key = md5($this->url()); } elseif (strpos($key, ']')) { if ('[' . $this->ext() . ']' == $key) { // 缓存某个后缀的请求 $key = md5($this->url()); } else { return; } } if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) { // 读取缓存 $response = Response::create()->code(304); throw new \think\exception\HttpResponseException($response); } elseif (Cache::has($key)) { list($content, $header) = Cache::get($key); $response = Response::create($content)->header($header); throw new \think\exception\HttpResponseException($response); } else { $this->cache = [$key, $expire]; } } }
/** * 缓存管理 * @param mixed $name 缓存名称,如果为数组表示进行缓存设置 * @param mixed $value 缓存值 * @param mixed $options 缓存参数 * @param string $tag 缓存标签 * @return mixed */ function cache($name, $value = '', $options = null, $tag = null) { if (is_array($options)) { // 缓存操作的同时初始化 Cache::connect($options); } elseif (is_array($name)) { // 缓存初始化 return Cache::connect($name); } if ('' === $value) { // 获取缓存 return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name); } elseif (is_null($value)) { // 删除缓存 return Cache::rm($name); } else { // 缓存数据 if (is_array($options)) { $expire = isset($options['expire']) ? $options['expire'] : null; //修复查询缓存无法设置过期时间 } else { $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 } if (is_null($tag)) { return Cache::set($name, $value, $expire); } else { return Cache::tag($tag)->set($name, $value, $expire); } } }
/** * 延时更新检查 返回false表示需要延时 * 否则返回实际写入的数值 * @access protected * @param string $type 自增或者自减 * @param string $guid 写入标识 * @param integer $step 写入步进值 * @param integer $lazyTime 延时时间(s) * @return false|integer */ protected function lazyWrite($type, $guid, $step, $lazyTime) { if (!Cache::has($guid . '_time')) { // 计时开始 Cache::set($guid . '_time', $_SERVER['REQUEST_TIME'], 0); Cache::$type($guid, $step, 0); } elseif ($_SERVER['REQUEST_TIME'] > Cache::get($guid . '_time') + $lazyTime) { // 删除缓存 $value = Cache::$type($guid, $step, 0); Cache::rm($guid); Cache::rm($guid . '_time'); return 0 === $value ? false : $value; } else { // 更新缓存 Cache::$type($guid, $step, 0); } return false; }
/** * 检查编译缓存是否存在 * @access public * @param string $cacheId 缓存的id * @return boolean */ public function isCache($cacheId) { if ($cacheId && $this->config['display_cache']) { // 缓存页面输出 return Cache::has($cacheId); } return false; }
/** * 设置当前地址的请求缓存 * @access public * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param mixed $expire 缓存有效期 * @return void */ public function cache($key, $expire = null) { if (false !== $key && $this->isGet() && !$this->isCheckCache) { // 标记请求缓存检查 $this->isCheckCache = true; if (false === $expire) { // 关闭当前缓存 return; } if ($key instanceof \Closure) { $key = call_user_func_array($key, [$this]); } elseif (true === $key) { // 自动缓存功能 $key = '__URL__'; } elseif (strpos($key, '|')) { list($key, $fun) = explode('|', $key); } // 特殊规则替换 if (false !== strpos($key, '__')) { $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url())], $key); } if (false !== strpos($key, ':')) { $param = $this->param(); foreach ($param as $item => $val) { if (is_string($val) && false !== strpos($key, ':' . $item)) { $key = str_replace(':' . $item, $val, $key); } } } elseif (strpos($key, ']')) { if ('[' . $this->ext() . ']' == $key) { // 缓存某个后缀的请求 $key = md5($this->url()); } else { return; } } if (isset($fun)) { $key = $fun($key); } if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) { // 读取缓存 $response = Response::create()->code(304); throw new \think\exception\HttpResponseException($response); } elseif (Cache::has($key)) { list($content, $header) = Cache::get($key); $response = Response::create($content)->header($header); throw new \think\exception\HttpResponseException($response); } else { $this->cache = [$key, $expire]; } } }
/** * 读取或者设置缓存 * @access public * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id * @param mixed $expire 缓存有效期 * @return mixed */ public function cache($key, $expire = null) { if (false !== strpos($key, ':')) { $param = $this->param(); foreach ($param as $item => $val) { if (is_string($val) && false !== strpos($key, ':' . $item)) { $key = str_replace(':' . $item, $val, $key); } } } if (Cache::has($key)) { // 读取缓存 $content = Cache::get($key); $response = Response::create($content)->code(304)->header('Content-Type', Cache::get($key . '_header')); throw new \think\exception\HttpResponseException($response); } else { $this->cache = [$key, $expire]; } }