Browse Source

refactor: redis function separated

master
Dnomd343 3 years ago
parent
commit
01b539795c
  1. 1
      main.php
  2. 36
      models/ntpCheck.php
  3. 62
      models/tgDC.php
  4. 38
      redisCache.php

1
main.php

@ -1,6 +1,7 @@
<?php <?php
require_once 'cmdRoute.php'; require_once 'cmdRoute.php';
require_once 'redisCache.php';
require_once 'tgInterface.php'; require_once 'tgInterface.php';
$env = loadEnv(); $env = loadEnv();

36
models/ntpCheck.php

@ -24,7 +24,7 @@ class ntpList {
return $data; return $data;
} }
private function hashGroupName($str) { // 计算组名的哈希值 private function hashGroupName($str) { // 计算组名的哈希值 取前12位
return substr(md5($str), 0, 12); return substr(md5($str), 0, 12);
} }
@ -70,13 +70,6 @@ class ntpList {
} }
class ntpCheck { class ntpCheck {
private $redisSetting = array( // redis缓存配置
'host' => '127.0.0.1',
'port' => 6379,
'passwd' => '',
'prefix' => 'ntp-'
);
private function isHost($host) { // 判断host是否合法 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); 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) { // 域名 if (count($match) !== 0) { // 域名
@ -100,28 +93,6 @@ class ntpCheck {
return $content; 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服务器状态 private function getNtpStatus($host) { // 获取NTP服务器状态
$html = $this->curlPost('https://servertest.online/ntp', array( $html = $this->curlPost('https://servertest.online/ntp', array(
'a' => $host, 'a' => $host,
@ -182,10 +153,11 @@ class ntpCheck {
} }
private function ntpStatus($host) { // 检测NTP服务器状态 带缓存 private function ntpStatus($host) { // 检测NTP服务器状态 带缓存
$servers = $this->getRedisData($host); // 查询缓存数据 $redis = new redisCache('ntp');
$servers = $redis->getData($host); // 查询缓存数据
if (!$servers) { // 缓存未命中 if (!$servers) { // 缓存未命中
$servers = $this->getNtpStatus($host); // 发起测试 $servers = $this->getNtpStatus($host); // 发起测试
$this->setRedisData($host, json_encode($servers), 300000); // 缓存5min $redis->setData($host, json_encode($servers), 300); // 缓存5min
} else { // 缓存命中 } else { // 缓存命中
$servers = json_decode($servers, true); // 使用缓存数据 $servers = json_decode($servers, true); // 使用缓存数据
} }

62
models/tgDC.php

@ -1,13 +1,6 @@
<?php <?php
class tgDC { class tgDC {
const redisSetting = array( // redis缓存配置
'host' => '127.0.0.1',
'port' => 6379,
'passwd' => '',
'prefix' => 'tgdc-'
);
private function getDcDetail($dc) { // 获取DC信息 private function getDcDetail($dc) { // 获取DC信息
switch ($dc) { switch ($dc) {
case 'DC1': 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 = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeOut);
$content = curl_exec($curl); $content = curl_exec($curl);
curl_close($curl); curl_close($curl);
return $content; 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) { // 检查用户名是否合法 private function checkAccount($account) { // 检查用户名是否合法
preg_match('/^[a-zA-Z0-9_]+$/', $account, $match); preg_match('/^[a-zA-Z0-9_]+$/', $account, $match);
if (count($match) === 0 or strlen($account) < 5) { // 用户名由至少5位 0-9/a-z/A-Z/_ 组成 if (count($match) === 0 or strlen($account) < 5) { // 用户名由至少5位 0-9/a-z/A-Z/_ 组成
@ -78,7 +49,7 @@ class tgDC {
return true; return true;
} }
private function getTgUserInfo($account) { // 获取Telegram用户信息 private function getUserInfo($account) { // 获取Telegram用户信息
$info['account'] = $account; $info['account'] = $account;
$info['name'] = null; $info['name'] = null;
$info['dc'] = null; $info['dc'] = null;
@ -86,17 +57,15 @@ class tgDC {
$html = $this->curl('https://t.me/' . $account); // 获取原始HTML数据 $html = $this->curl('https://t.me/' . $account); // 获取原始HTML数据
$html = preg_replace('/[\t\n\r]+/', '', $html); // 去除干扰 $html = preg_replace('/[\t\n\r]+/', '', $html); // 去除干扰
if (!is_string($html) || $html == '') { return $info; } // 用户名无效 if (!is_string($html) || $html == '') { return $info; } // 用户名无效
$avatarRegex = '/<img class="tgme_page_photo_image" src="([^<>]+)">/'; $avatarRegex = '/<img class="tgme_page_photo_image" src="([^<>]+)">/';
$nameRegex = '/<span dir="auto">(.+?)<\/span>/'; $nameRegex = '/<span dir="auto">(.+?)<\/span>/';
preg_match_all($avatarRegex, $html, $avatarMatch); // 匹配目标头像 preg_match($avatarRegex, $html, $avatarMatch); // 匹配目标头像
preg_match_all($nameRegex, $html, $nameMatch); // 匹配目标名称 preg_match($nameRegex, $html, $nameMatch); // 匹配目标名称
if ($nameMatch[1]) { if ($nameMatch[1]) {
$info['name'] = $nameMatch[1][0]; // 获取用户名 $info['name'] = $nameMatch[1]; // 获取用户名
} }
if ($avatarMatch[1]) { if ($avatarMatch[1]) {
$avatarUrl = $avatarMatch[1][0]; // 获取头像链接 $avatarUrl = $avatarMatch[1]; // 获取头像链接
} }
if ($avatarUrl) { // 头像存在 if ($avatarUrl) { // 头像存在
$dcRegex = '/https:\/\/cdn(.+)\.telesco\.pe\//'; $dcRegex = '/https:\/\/cdn(.+)\.telesco\.pe\//';
@ -111,18 +80,19 @@ class tgDC {
return $info; return $info;
} }
private function getUserInfo($account) { // 获取用户信息,带缓存 private function getUserInfoCache($account) { // 获取用户信息 带缓存
$info = $this->getRedisData($account); // 查询缓存数据 $redis = new redisCache('tgdc');
$info = $redis->getData($account); // 查询缓存数据
if (!$info) { // 缓存未命中 if (!$info) { // 缓存未命中
$info = $this->getTgUserInfo($account); // 发起查询 $info = $this->getUserInfo($account); // 发起查询
if (!$info['name'] && !$info['dc']) { // 用户名与头像均无 if (!$info['name'] && !$info['dc']) { // 用户名与头像均无
$cacheTTL = 120000; // 缓存2min $cacheTTL = 120; // 缓存2min
} else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像 } else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像
$cacheTTL = 20000; // 缓存20s $cacheTTL = 20; // 缓存20s
} else { } else {
$cacheTTL = 600000; // 其余情况缓存10min $cacheTTL = 3600; // 其余情况缓存1h
} }
$this->setRedisData($account, json_encode($info), $cacheTTL); // 缓存数据 $redis->setData($account, json_encode($info), $cacheTTL); // 缓存数据
} else { // 缓存命中 } else { // 缓存命中
$info = json_decode($info, true); // 使用缓存数据 $info = json_decode($info, true); // 使用缓存数据
} }
@ -138,7 +108,7 @@ class tgDC {
'text' => '用户名无效' 'text' => '用户名无效'
); );
} }
$info = $this->getUserInfo($account); $info = $this->getUserInfoCache($account);
if (!$info['name'] && !$info['dc']) { // 用户名与头像均无 if (!$info['name'] && !$info['dc']) { // 用户名与头像均无
return array( return array(
'text' => '@' . $info['account'] . ' 无法识别' 'text' => '@' . $info['account'] . ' 无法识别'

38
redisCache.php

@ -0,0 +1,38 @@
<?php
class redisCache {
private $redisSetting = array( // redis接口
'host' => '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;
}
}
?>
Loading…
Cancel
Save