Skip to content

Commit 76cd260

Browse files
committed
[redis] fix crash on invalid credentials
1 parent 6a1d9b7 commit 76cd260

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

cache.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,34 @@ function __construct($search = null) { parent::__construct('user', $search); }
9898

9999
if (ENABLE_REDIS) {
100100
$redis = new Redis();
101-
$redis->connect(REDIS_HOST, REDIS_PORT);
102101

103-
if (!empty(REDIS_PASSWORD))
104-
$redis->auth(REDIS_PASSWORD);
102+
try {
103+
$redis->connect(REDIS_HOST, REDIS_PORT);
105104

106-
$redis_db = 0;
107-
$redis_dbs = $redis->config('GET', 'databases');
108-
$redis_db_select = !is_numeric(REDIS_DATABASE) && !empty($redis_dbs);
109-
$redis_memory = $redis->info('memory');
105+
if (!empty(REDIS_PASSWORD))
106+
$redis->auth(REDIS_PASSWORD);
110107

111-
if (!$redis_db_select)
112-
$redis_db = REDIS_DATABASE;
113-
else if (!empty($_COOKIE['redis_db']))
114-
$redis_db = (int)$_COOKIE['redis_db'];
108+
$redis_db = 0;
109+
$redis_dbs = $redis->config('GET', 'databases');
110+
$redis_db_select = !is_numeric(REDIS_DATABASE) && !empty($redis_dbs);
111+
$redis_memory = $redis->info('memory');
115112

116-
$redis->select($redis_db);
113+
if (!$redis_db_select)
114+
$redis_db = REDIS_DATABASE;
115+
else if (!empty($_COOKIE['redis_db']))
116+
$redis_db = (int)$_COOKIE['redis_db'];
117117

118-
if( is_action('redis_clear') ) {
118+
$redis->select($redis_db);
119+
} catch(RedisException $ex) {
120+
// Failed to connect
121+
}
122+
123+
if( $redis->isConnected() && is_action('redis_clear') ) {
119124
$redis->flushDb();
120125
redirect('?');
121126
}
122127

123-
if( is_action('redis_delete') ) {
128+
if( $redis->isConnected() && is_action('redis_delete') ) {
124129
$list = redis_keys(get_selector());
125130

126131
foreach ($list as $key => $item)
@@ -817,7 +822,7 @@ function sort_list($list) {
817822
</div>
818823
<?php endif; ?>
819824

820-
<?php if(ENABLE_REDIS): ?>
825+
<?php if(ENABLE_REDIS && $redis->isConnected()): ?>
821826
<h2 id="redis">Redis</h2>
822827
<div>
823828
<h3>Memory <?=human_size(redis_mem('used') + redis_mem('hash'))?> of <?=human_size(redis_mem('total'))?></h3>

demo/index.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ class CacheDemoClass {
4545
$memcache->add('type.array', ['abc', 'def']);
4646
$memcache->add('type.string', 'hello-world');
4747
$memcache->add('type.ttl.string', 'hello-world', time() + 3600);
48+
49+
// Redis configuration
50+
$redis_host = getenv('REDIS_HOST') ?: '127.0.0.1';
51+
$redis_port = getenv('REDIS_PORT') ?: 6379;
52+
$redis_password = getenv('REDIS_PASSWORD') ?: null;
53+
$redis_database = getenv('REDIS_DATABASE') ?: null;
54+
55+
$redis = new Redis();
56+
try {
57+
$redis->connect($redis_host, $redis_port);
58+
59+
if (!empty($redis_password))
60+
$redis->auth($redis_password);
61+
62+
if (!empty($redis_database))
63+
$redis->select($redis_database);
64+
65+
$redis->sAdd('type.set', 'abc', 'def');
66+
$redis->set('type.string', 'hello-world');
67+
$redis->setEx('type.ttl.string', 3600, 'hello-world');
68+
} catch(Exception $ex) {}
69+
4870
}
4971

5072
require_once('../cache.php');

0 commit comments

Comments
 (0)