/** * 初始化钉钉 */ 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); }
/** * 获取jsticket. * * @return string */ public function getTicket() { $key = 'pakey_wechat_jsapi_ticket.' . $this->appId; // for php 5.3 $appId = $this->appId; $appSecret = $this->appSecret; $cache = $this->cache; $apiTicket = self::API_TICKET; return $this->cache->get($key, function ($key) use($appId, $appSecret, $cache, $apiTicket) { $http = new Http(new AccessToken($appId, $appSecret)); $result = $http->get($apiTicket); $cache->set($key, $result['ticket'], $result['expires_in']); return $result['ticket']; }); }
/** * 初始化微信 * @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); }
/** * 读取或者设置缓存 * @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); } } }
/** * 删除记录 * @access public * @param mixed $data 表达式 true 表示强制删除 * @return int * @throws Exception * @throws PDOException */ public function delete($data = null) { // 分析查询表达式 $options = $this->parseExpress(); if (isset($options['cache']) && is_string($options['cache'])) { $key = $options['cache']; } if (!is_null($data) && true !== $data) { if (!isset($key) && !is_array($data)) { // 缓存标识 $key = 'think:' . $options['table'] . '|' . $data; } // AR模式分析主键条件 $this->parsePkWhere($data, $options); } if (true !== $data && empty($options['where'])) { // 如果条件为空 不进行删除操作 除非设置 1=1 throw new Exception('delete without condition'); } // 生成删除SQL语句 $sql = $this->builder()->delete($options); // 获取参数绑定 $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } // 检测缓存 if (isset($key) && Cache::get($key)) { // 删除缓存 Cache::rm($key); } // 执行操作 return $this->execute($sql, $bind); }
/** * 渲染模板文件 * @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; }
/** * 检查编译缓存是否存在 * @access public * @param string $cacheId 缓存的id * @return boolean */ public function isCache($cacheId) { if ($cacheId && $this->config['display_cache']) { // 缓存页面输出 return Cache::get($cacheId) ? true : false; } return false; }
/** * 读取或者设置缓存 * @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]; } }
/** * 设置当前地址的请求缓存 * @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]; } } }
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; } }
/** * 缓存管理 * @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); } }
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 * @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; }