Skip to content

Commit 0e0175e

Browse files
xuliangTangTeakowa
andauthored
refactor: redis连接配置读取env (#4)
* refactor: redis连接配置读取env * chore: add testing Signed-off-by: Teakowa Gatanothor O'deorain <hub+git@teakowa.dev> Co-authored-by: Teakowa Gatanothor O'deorain <hub+git@teakowa.dev>
1 parent 93b6131 commit 0e0175e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

.env.testing

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
REDIS_HOST=127.0.0.1
2+
REDIS_PASSWORD=
3+
REDIS_PORT=6379

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ jobs:
8989

9090
- name: Prepare for environment variables
9191
run: |
92+
cp .env.testing .env
9293
composer dump-autoload
9394
9495
- name: Run Testsuite
9596
run: vendor/bin/pest tests/ --coverage-text --colors=always --coverage-html=coverage --coverage-clover coverage.xml
97+
env:
98+
REDIS_HOST: 127.0.0.1
99+
REDIS_PASSWORD:
100+
REDIS_PORT: 6379
96101

97102
- name: Upload coverage to Codecov
98103
uses: codecov/codecov-action@v2

src/Redis.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ public function __construct(array $config = [])
1818
$this->type = 'string';
1919
$this->score = 0;
2020
$this->expire = -1;
21+
22+
$options = [
23+
'parameters' => [
24+
'password' => $config['password'] ?? getenv('REDIS_PASSWORD') ?? '',
25+
'database' => $config['database'] ?? getenv('REDIS_DB') ?? 0,
26+
],
27+
];
2128
$this->client = new Client([
2229
'scheme' => 'tcp',
23-
'host' => $config['host'] ?? '127.0.0.1',
24-
'port' => $config['port'] ?? 6379,
25-
]);
30+
'host' => $config['host'] ?? getenv('REDIS_HOST') ?? '127.0.0.1',
31+
'port' => $config['port'] ?? getenv('REDIS_PORT') ?? 6379,
32+
], $options);
2633
}
2734

2835
/**
2936
* 调用predis方法.
3037
*
38+
* @param string $name
3139
* @param array $arguments
3240
*/
3341
public function __call(string $name, array $arguments): void

0 commit comments

Comments
 (0)