From 771af284118fc74e929e49245b3c308fbb84733c Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 11 Jul 2021 19:50:23 +0800 Subject: [PATCH] feat: redis cache support --- backend/queryInfo.php | 18 +++++++++++++++--- backend/redis.php | 32 ++++++++++++++++++++++++++++++++ docs/setup.md | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 backend/redis.php diff --git a/backend/queryInfo.php b/backend/queryInfo.php index 48ab864..39b863e 100644 --- a/backend/queryInfo.php +++ b/backend/queryInfo.php @@ -1,7 +1,8 @@ true, + 'host' => '127.0.0.1', + 'port' => 6379, + 'passwd' => '', + 'prefix' => 'echoip-', + 'cache_time' => 3600000 +); + +function getRedisData($ip) { // 查询Redis,不存在返回NULL + $redis = new Redis(); + global $redisSetting; + $redis->connect($redisSetting['host'], $redisSetting['port']); + $redis->auth($redisSetting['passwd']); + $redisKey = $redisSetting['prefix'] . $ip; + $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; + return $redisValue; +} + +function setRedisData($ip, $data) { // 写入信息到Redis + $redis = new Redis(); + global $redisSetting; + $redis->connect($redisSetting['host'], $redisSetting['port']); + $redis->auth($redisSetting['passwd']); + $redisKey = $redisSetting['prefix'] . $ip; + $redis->set($redisKey, $data); // 写入数据库 + $redis->pexpire($redisKey, $redisSetting['cache_time']); // 设置过期时间 +} + +?> \ No newline at end of file diff --git a/docs/setup.md b/docs/setup.md index 5d005d1..6058e63 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -39,6 +39,25 @@ shell> systemctl | grep fpm php7.3-fpm.service loaded active running The PHP 7.3 FastCGI Process Manager ``` +确认Redis正常运行 + +``` +shell> redis-cli --version +···Redis版本信息··· + +# 登录redis服务 +shell> redis-cli +# 若服务主机非默认参数,使用以下命令登录 +shell> redis-cli -h {hostname} -p {port} + +# 若配置有密码则先认证 +127.0.0.1:6379> auth {passwd} + +# 登录后确认连接 +127.0.0.1:6379> ping +PONG +``` + ### 3. qqwry.dat配置 获取并解密纯真IP数据库 @@ -59,7 +78,25 @@ shell> cd /var/www/echoIP/backend/qqwryFormat shell> ./start.sh ``` -### 4. 配置Web服务 +### 4. 配置Redis连接 + +Redis连接参数位于 `backend/redis.php` 文件中,默认如下 + +``` +$redisSetting = array( + 'enable' => true, + 'host' => '127.0.0.1', + 'port' => 6379, + 'passwd' => '', + 'prefix' => 'echoip-', + 'cache_time' => 3600000 +); +``` + +按当前服务器配置修改,`enable` 为false时可关闭缓存功能,无密码时将 `passwd` 留空即可,键值前缀与缓存时间(单位ms)按实际需要修改。 + + +### 5. 配置Web服务 配置网页服务器代理,需要额外占用除80与443之外的一个端口,默认为TCP/1601,可按需修改。这里使用Nginx作为示例,其他Web服务原理类似。