From df978dcd47064920ce20ade590353cc3a239a7c8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 10 Aug 2021 21:28:57 +0800 Subject: [PATCH] update: redis support for ntp check --- models/ntpCheck.php | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/models/ntpCheck.php b/models/ntpCheck.php index 7f8af08..063ab15 100644 --- a/models/ntpCheck.php +++ b/models/ntpCheck.php @@ -1,6 +1,13 @@ '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) { // 域名 @@ -24,6 +31,28 @@ 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, @@ -74,8 +103,19 @@ class ntpCheck { } } + private function ntpStatus($host) { // 检测NTP服务器状态 带缓存 + $servers = $this->getRedisData($host); // 查询缓存数据 + if (!$servers) { // 缓存未命中 + $servers = $this->getNtpStatus($host); // 发起测试 + $this->setRedisData($host, json_encode($servers), 300000); // 缓存5min + } else { // 缓存命中 + $servers = json_decode($servers, true); // 使用缓存数据 + } + return $servers; + } + public function checkNtp($host) { - $servers = $this->getNtpStatus($host); + $servers = $this->ntpStatus($host); if (count($servers) === 0) { $msg = '`' . $host . '`' . PHP_EOL; $msg .= 'NTP Server *Offline*' . PHP_EOL . PHP_EOL;