Browse Source

refactor: more powerful .env settings

master
Dnomd343 3 years ago
parent
commit
e63ecdf7c0
  1. 9
      .env.example
  2. 5
      main.php
  3. 16
      redisCache.php

9
.env.example

@ -0,0 +1,9 @@
# Bot Settings
BOT_NAME=
BOT_TOKEN=
# Redis Settings
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWD=
REDIS_PREFIX=tgbot

5
main.php

@ -51,7 +51,10 @@ function loadEnv() { // 载入环境变量
$file = fopen('.env', 'r');
$data = array();
while (!feof($file)) { // 逐行读入文件
$record = explode('=', trim(fgets($file)));
$raw = trim(fgets($file));
if ($raw == '') { continue; } // 跳过空行
if (substr($raw, 0, 1) === '#') { continue; } // 跳过注释
$record = explode('=', $raw);
if (count($record) === 2) { // 合法记录
$data[$record[0]] = $record[1];
}

16
redisCache.php

@ -1,14 +1,16 @@
<?php
class redisCache {
private $redisSetting = array( // redis接口
'host' => '127.0.0.1',
'port' => 6379,
'passwd' => ''
);
private $redisSetting = array(); // redis接口参数
public function __construct($prefix) { // 类构建时指定前缀
$this->redisSetting['prefix'] = 'tgbot-' . $prefix . '-';
public function __construct($prefix) { // 类初始化
global $env;
$this->redisSetting = array(
'host' => $env['REDIS_HOST'],
'port' => $env['REDIS_PORT'],
'passwd' => $env['REDIS_PASSWD'],
'prefix' => $env['REDIS_PREFIX'] . '-' . $prefix . '-'
);
}
public function getData($key) { // 查询Redis缓存,不存在返回NULL

Loading…
Cancel
Save