diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..4d833f1 --- /dev/null +++ b/.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 diff --git a/main.php b/main.php index 832ab5a..a2ca512 100644 --- a/main.php +++ b/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]; } diff --git a/redisCache.php b/redisCache.php index 8f601de..a7c6e27 100644 --- a/redisCache.php +++ b/redisCache.php @@ -1,16 +1,18 @@ '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 $redis = new Redis(); $redis->connect($this->redisSetting['host'], $this->redisSetting['port']);