diff --git a/main.php b/main.php index 5a5f17a..832ab5a 100644 --- a/main.php +++ b/main.php @@ -1,6 +1,7 @@ '127.0.0.1', - 'port' => 6379, - 'passwd' => '', - 'prefix' => 'ntp-' - ); - private function isHost($host) { // 判断host是否合法 preg_match('/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/', $host, $match); if (count($match) !== 0) { // 域名 @@ -100,28 +93,6 @@ class ntpCheck { return $content; } - private function getRedisData($host) { // 查询Redis缓存,不存在返回NULL - $redis = new Redis(); - $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); - if ($this->redisSetting['passwd'] != '') { - $redis->auth($this->redisSetting['passwd']); - } - $redisKey = $this->redisSetting['prefix'] . $host; - $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; - return $redisValue; - } - - private function setRedisData($host, $data, $cacheTTL) { // 写入信息到Redis缓存 - $redis = new Redis(); - $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); - if ($this->redisSetting['passwd'] != '') { - $redis->auth($this->redisSetting['passwd']); - } - $redisKey = $this->redisSetting['prefix'] . $host; - $redis->set($redisKey, $data); // 写入数据库 - $redis->pexpire($redisKey, $cacheTTL); // 设置过期时间 - } - private function getNtpStatus($host) { // 获取NTP服务器状态 $html = $this->curlPost('https://servertest.online/ntp', array( 'a' => $host, @@ -182,10 +153,11 @@ class ntpCheck { } private function ntpStatus($host) { // 检测NTP服务器状态 带缓存 - $servers = $this->getRedisData($host); // 查询缓存数据 + $redis = new redisCache('ntp'); + $servers = $redis->getData($host); // 查询缓存数据 if (!$servers) { // 缓存未命中 $servers = $this->getNtpStatus($host); // 发起测试 - $this->setRedisData($host, json_encode($servers), 300000); // 缓存5min + $redis->setData($host, json_encode($servers), 300); // 缓存5min } else { // 缓存命中 $servers = json_decode($servers, true); // 使用缓存数据 } diff --git a/models/tgDC.php b/models/tgDC.php index cc87c6a..061311e 100644 --- a/models/tgDC.php +++ b/models/tgDC.php @@ -1,13 +1,6 @@ '127.0.0.1', - 'port' => 6379, - 'passwd' => '', - 'prefix' => 'tgdc-' - ); - private function getDcDetail($dc) { // 获取DC信息 switch ($dc) { case 'DC1': @@ -35,38 +28,16 @@ class tgDC { } } - private function curl($url) { // curl模拟 5s超时 + private function curl($url, $timeOut = 5) { // curl模拟 默认5s超时 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeOut); $content = curl_exec($curl); curl_close($curl); return $content; } - private function getRedisData($account) { // 查询Redis缓存,不存在返回NULL - $redis = new Redis(); - $redis->connect(tgDC::redisSetting['host'], tgDC::redisSetting['port']); - if (tgDC::redisSetting['passwd'] != '') { - $redis->auth(tgDC::redisSetting['passwd']); - } - $redisKey = tgDC::redisSetting['prefix'] . $account; - $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; - return $redisValue; - } - - private function setRedisData($account, $data, $cacheTTL = 600000) { // 写入信息到Redis缓存 - $redis = new Redis(); - $redis->connect(tgDC::redisSetting['host'], tgDC::redisSetting['port']); - if (tgDC::redisSetting['passwd'] != '') { - $redis->auth(tgDC::redisSetting['passwd']); - } - $redisKey = tgDC::redisSetting['prefix'] . $account; - $redis->set($redisKey, $data); // 写入数据库 - $redis->pexpire($redisKey, $cacheTTL); // 设置过期时间 默认十分钟 - } - private function checkAccount($account) { // 检查用户名是否合法 preg_match('/^[a-zA-Z0-9_]+$/', $account, $match); if (count($match) === 0 or strlen($account) < 5) { // 用户名由至少5位 0-9/a-z/A-Z/_ 组成 @@ -78,7 +49,7 @@ class tgDC { return true; } - private function getTgUserInfo($account) { // 获取Telegram用户信息 + private function getUserInfo($account) { // 获取Telegram用户信息 $info['account'] = $account; $info['name'] = null; $info['dc'] = null; @@ -86,17 +57,15 @@ class tgDC { $html = $this->curl('https://t.me/' . $account); // 获取原始HTML数据 $html = preg_replace('/[\t\n\r]+/', '', $html); // 去除干扰 if (!is_string($html) || $html == '') { return $info; } // 用户名无效 - $avatarRegex = '//'; $nameRegex = '/(.+?)<\/span>/'; - preg_match_all($avatarRegex, $html, $avatarMatch); // 匹配目标头像 - preg_match_all($nameRegex, $html, $nameMatch); // 匹配目标名称 - + preg_match($avatarRegex, $html, $avatarMatch); // 匹配目标头像 + preg_match($nameRegex, $html, $nameMatch); // 匹配目标名称 if ($nameMatch[1]) { - $info['name'] = $nameMatch[1][0]; // 获取用户名 + $info['name'] = $nameMatch[1]; // 获取用户名 } if ($avatarMatch[1]) { - $avatarUrl = $avatarMatch[1][0]; // 获取头像链接 + $avatarUrl = $avatarMatch[1]; // 获取头像链接 } if ($avatarUrl) { // 头像存在 $dcRegex = '/https:\/\/cdn(.+)\.telesco\.pe\//'; @@ -111,18 +80,19 @@ class tgDC { return $info; } - private function getUserInfo($account) { // 获取用户信息,带缓存 - $info = $this->getRedisData($account); // 查询缓存数据 + private function getUserInfoCache($account) { // 获取用户信息 带缓存 + $redis = new redisCache('tgdc'); + $info = $redis->getData($account); // 查询缓存数据 if (!$info) { // 缓存未命中 - $info = $this->getTgUserInfo($account); // 发起查询 + $info = $this->getUserInfo($account); // 发起查询 if (!$info['name'] && !$info['dc']) { // 用户名与头像均无 - $cacheTTL = 120000; // 缓存2min + $cacheTTL = 120; // 缓存2min } else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像 - $cacheTTL = 20000; // 缓存20s + $cacheTTL = 20; // 缓存20s } else { - $cacheTTL = 600000; // 其余情况缓存10min + $cacheTTL = 3600; // 其余情况缓存1h } - $this->setRedisData($account, json_encode($info), $cacheTTL); // 缓存数据 + $redis->setData($account, json_encode($info), $cacheTTL); // 缓存数据 } else { // 缓存命中 $info = json_decode($info, true); // 使用缓存数据 } @@ -138,7 +108,7 @@ class tgDC { 'text' => '用户名无效' ); } - $info = $this->getUserInfo($account); + $info = $this->getUserInfoCache($account); if (!$info['name'] && !$info['dc']) { // 用户名与头像均无 return array( 'text' => '@' . $info['account'] . ' 无法识别' diff --git a/redisCache.php b/redisCache.php new file mode 100644 index 0000000..8f601de --- /dev/null +++ b/redisCache.php @@ -0,0 +1,38 @@ + '127.0.0.1', + 'port' => 6379, + 'passwd' => '' + ); + + public function __construct($prefix) { // 类构建时指定前缀 + $this->redisSetting['prefix'] = 'tgbot-' . $prefix . '-'; + } + + public function getData($key) { // 查询Redis缓存,不存在返回NULL + $redis = new Redis(); + $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); + if ($this->redisSetting['passwd'] !== '') { + $redis->auth($this->redisSetting['passwd']); // 密码认证 + } + $redisKey = $this->redisSetting['prefix'] . $key; + $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; + return $redisValue; + } + + public function setData($key, $data, $cacheTTL = 600) { // 写入信息到Redis缓存 默认10min过期 + $redis = new Redis(); + $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); + if ($this->redisSetting['passwd'] !== '') { + $redis->auth($this->redisSetting['passwd']); // 密码认证 + } + $redisKey = $this->redisSetting['prefix'] . $key; + $status = $redis->set($redisKey, $data); // 写入数据库 + $redis->pexpire($redisKey, $cacheTTL * 1000); // 设置过期时间 单位ms + return $status; + } +} + +?>