/** * 初始化钉钉 */ protected function _initDing() { $config = Config::get('dingtalk_config'); Ding::init($config); $token = Cache::get('dingtalk_access_token'); if (!$token) { $token = Ding::getAccessToken(); Cache::set('dingtalk_access_token', $token, 7000); } Ding::setConfig('access_token', $token); }
/** * 初始化微信 * @return void */ protected function initWechat() { $token = Cache::get('wechat_access_token'); $config = Config::get('wechat_config'); // 检测TOKEN是否过期 if (!$token) { Wechat::instance($config); $token = Token::get(); // 重新设置TOKEN并缓存,重新初始化微信 Cache::set('wechat_access_token', Token::get(), 7200); $config['access_token'] = $token; Wechat::instance($config, true); } else { $config['access_token'] = $token; Wechat::instance($config); } }
/** * 加载系统扩展配置 */ public static function load() { $config = \think\Cache::get('db_config_cache_data'); if (!$config) { // 在这里先判断一下数据库是否已经正确安装 $Db = \think\Loader::db(); $Query = $Db->query("SHOW TABLES LIKE '" . \think\Config::get('database.prefix') . "config'"); if (empty($Query)) { self::install(); } $data = \think\Db::name('Config')->where('status', 1)->field('type,name,value')->select(); $config = []; if ($data && is_array($data)) { foreach ($data as $value) { $config[$value['name']] = self::parse($value['type'], $value['value']); } } \think\Cache::set('db_config_cache_data', $config); } \think\Config::set($config); }
/** * 缓存管理 * @param mixed $name 缓存名称,如果为数组表示进行缓存设置 * @param mixed $value 缓存值 * @param mixed $options 缓存参数 * @return mixed */ function S($name, $value = '', $options = null) { if (is_array($options)) { // 缓存操作的同时初始化 \think\Cache::connect($options); } elseif (is_array($name)) { // 缓存初始化 return \think\Cache::connect($name); } if ('' === $value) { // 获取缓存 return \think\Cache::get($name); } elseif (is_null($value)) { // 删除缓存 return \think\Cache::rm($name); } else { // 缓存数据 if (is_array($options)) { $expire = isset($options['expire']) ? $options['expire'] : null; //修复查询缓存无法设置过期时间 } else { $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 } return \think\Cache::set($name, $value, $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); } } }
/** * 查找单条记录 * @access public * @param array|string|Query|\Closure $data * @return array|false|\PDOStatement|string|Model * @throws DbException * @throws ModelNotFoundException * @throws DataNotFoundException */ public function find($data = null) { if ($data instanceof Query) { return $data->find(); } elseif ($data instanceof \Closure) { call_user_func_array($data, [&$this]); $data = null; } // 分析查询表达式 $options = $this->parseExpress(); if (!is_null($data)) { // AR模式分析主键条件 $this->parsePkWhere($data, $options); } $options['limit'] = 1; $result = false; if (empty($options['fetch_sql']) && !empty($options['cache'])) { // 判断查询缓存 $cache = $options['cache']; if (true === $cache['key'] && !is_null($data) && !is_array($data)) { $key = 'think:' . $options['table'] . '|' . $data; } else { $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); } $result = Cache::get($key); } if (!$result) { // 生成查询SQL $sql = $this->builder()->select($options); // 获取参数绑定 $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } // 执行查询 $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']); if ($result instanceof \PDOStatement) { // 返回PDOStatement对象 return $result; } if (isset($cache)) { // 缓存数据 if (isset($cache['tag'])) { Cache::tag($cache['tag'])->set($key, $result, $cache['expire']); } else { Cache::set($key, $result, $cache['expire']); } } } // 数据处理 if (!empty($result[0])) { $data = $result[0]; if (!empty($this->model)) { // 返回模型对象 $model = $this->model; $data = new $model($data); $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); if ($this->allowField) { $data->allowField($this->allowField); } // 关联查询 if (!empty($options['relation'])) { $data->relationQuery($options['relation']); } if (!empty($options['with'])) { // 预载入 $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); } } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); } else { $data = null; } return $data; }
/** * 渲染模板文件 * @access public * @param string $template 模板文件 * @param array $vars 模板变量 * @param array $config 模板参数 * @return void */ public function fetch($template, $vars = [], $config = []) { if ($vars) { $this->data = $vars; } if ($config) { $this->config($config); } if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 读取渲染缓存 $cacheContent = Cache::get($this->config['cache_id']); if (false !== $cacheContent) { echo $cacheContent; return; } } $template = $this->parseTemplateFile($template); if ($template) { $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . '.' . ltrim($this->config['cache_suffix'], '.'); if (!$this->checkCache($cacheFile)) { // 缓存无效 重新模板编译 $content = file_get_contents($template); $this->compiler($content, $cacheFile); } // 页面缓存 ob_start(); ob_implicit_flush(0); // 读取编译存储 $this->storage->read($cacheFile, $this->data); // 获取并清空缓存 $content = ob_get_clean(); if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 缓存页面输出 Cache::set($this->config['cache_id'], $content, $this->config['cache_time']); } echo $content; } }
private static function getRouteAlias() { if ($item = Cache::get('think_route_map')) { return $item; } // 获取路由定义 $array = Route::rules(); foreach ($array as $type => $rules) { foreach ($rules as $rule => $val) { if (true === $val || empty($val['rule'])) { continue; } $route = $val['route']; $vars = $val['var']; if (is_array($val['rule'])) { foreach ($val['rule'] as $val) { $key = $val['rule']; $route = $val['route']; $var = $val['var']; $param = []; if (is_array($route)) { $route = implode('\\', $route); } elseif ($route instanceof \Closure) { continue; } elseif (strpos($route, '?')) { list($route, $str) = explode('?', $route, 2); parse_str($str, $param); } $var = array_merge($vars, $var); $item[$route][] = [$rule . '/' . $key, $var, $param]; } } else { $param = []; if (is_array($route)) { $route = implode('\\', $route); } elseif ($route instanceof \Closure) { continue; } elseif (strpos($route, '?')) { list($route, $str) = explode('?', $route, 2); parse_str($str, $param); } $item[$route][] = [$rule, $vars, $param]; } } } // 检测路由别名 $alias = Route::rules('alias'); foreach ($alias as $rule => $route) { $route = is_array($route) ? $route[0] : $route; $item[$route][] = [$rule, [], []]; } !App::$debug && Cache::set('think_route_map', $item); return $item; }
public function testStaticCall() { $this->assertTrue(Cache::set('a', 1)); $this->assertEquals(1, Cache::get('a')); $this->assertEquals(2, Cache::inc('a')); $this->assertEquals(4, Cache::inc('a', 2)); $this->assertEquals(4, Cache::get('a')); $this->assertEquals(3, Cache::dec('a')); $this->assertEquals(1, Cache::dec('a', 2)); $this->assertEquals(1, Cache::get('a')); $this->assertNotNull(Cache::rm('a')); $this->assertNotNull(Cache::clear()); }
/** * 发送数据到客户端 * @access public * @return mixed * @throws \InvalidArgumentException */ public function send() { // 处理输出数据 $data = $this->getContent(); // Trace调试注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } if (200 == $this->code) { $cache = Request::instance()->getCache(); if ($cache) { header('Cache-Control: max-age=' . $cache[1] . ',must-revalidate'); header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Expires:' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT'); $header['Content-Type'] = $this->header['Content-Type']; Cache::set($cache[0], [$data, $header], $cache[1]); } } echo $data; if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } // 监听response_end Hook::listen('response_end', $this); }
public function testStaticCall() { $this->assertTrue(Cache::set('a', 'a')); $this->assertEquals('a', Cache::get('a')); $this->assertNotNull(Cache::rm('a')); }
/** * 延时更新检查 返回false表示需要延时 * 否则返回实际写入的数值 * @access public * @param string $guid 写入标识 * @param integer $step 写入步进值 * @param integer $lazyTime 延时时间(s) * @return false|integer */ protected function lazyWrite($guid, $step, $lazyTime) { if (false !== ($value = Cache::get($guid))) { // 存在缓存写入数据 if (NOW_TIME > Cache::get($guid . '_time') + $lazyTime) { // 延时更新时间到了,删除缓存数据 并实际写入数据库 Cache::rm($guid); Cache::rm($guid . '_time'); return $value + $step; } else { // 追加数据到缓存 Cache::set($guid, $value + $step); return false; } } else { // 没有缓存数据 Cache::set($guid, $step); // 计时开始 Cache::set($guid . '_time', NOW_TIME); return false; } }
/** * 查找单条记录 * @access public * @param array $data 表达式 * @return array|false|\PDOStatement|string|Model * @throws DbException * @throws Exception * @throws \think\exception\PDOException */ public function find($data = []) { if ($data instanceof Query) { return $data->find(); } elseif ($data instanceof \Closure) { call_user_func_array($data, [&$this]); } // 分析查询表达式 $options = $this->parseExpress(); if (empty($options['where']) && (!empty($data) || 0 == $data)) { // AR模式分析主键条件 $this->parsePkWhere($data, $options); } $options['limit'] = 1; $result = false; if (!empty($options['cache'])) { // 判断查询缓存 $cache = $options['cache']; $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); $result = Cache::get($key); } if (!$result) { // 生成查询SQL $sql = $this->builder()->select($options); // 执行查询 $result = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']); if (is_string($result)) { // 返回SQL return $result; } if ($result instanceof \PDOStatement) { // 返回PDOStatement对象 return $result; } if (isset($cache)) { // 缓存数据 Cache::set($key, $result, $cache['expire']); } } // 数据处理 if (!empty($result[0])) { $data = $result[0]; if (!empty($options['model'])) { // 返回模型对象 $data = new $options['model']($data); $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); // 关联查询 if (!empty($options['relation'])) { $data->relationQuery($options['relation']); } if (!empty($options['with'])) { // 预载入 $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); } } } elseif (!empty($options['fail'])) { throw new DbException('Data not Found', $options, $sql); } else { $data = false; } return $data; }
/** * 发送数据到客户端 * @access public * @return mixed * @throws \InvalidArgumentException */ public function send() { // 处理输出数据 $data = $this->getContent(); // Trace调试注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } echo $data; if (200 == $this->code) { $cache = Request::instance()->getCache(); if ($cache) { Cache::set($cache[0], $data, $cache[1]); Cache::set($cache[0] . '_header', $this->header['Content-Type']); } } if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } // 监听response_end Hook::listen('response_end', $this); }