Skip to content

Commit b00e8b9

Browse files
ddevsrpaulbalandan
andauthored
feat: add persistent config item to Session's Redis handler (#9793)
* feat: added persistent config item to redis handler Session * tests: persistent connection session redis fix: test persistent parameter false * refactor: using filter_var * refactor: name test * Update system/Session/Handlers/RedisHandler.php Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> * Fix filter_var on persistent * Fix phpstan error --------- Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent 21fbcf6 commit b00e8b9

File tree

3 files changed

+109
-56
lines changed

3 files changed

+109
-56
lines changed

system/Session/Handlers/RedisHandler.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,19 @@ protected function setSavePath(): void
142142
}
143143
}
144144

145-
$password = $query['auth'] ?? null;
146-
$database = isset($query['database']) ? (int) $query['database'] : 0;
147-
$timeout = isset($query['timeout']) ? (float) $query['timeout'] : 0.0;
148-
$prefix = $query['prefix'] ?? null;
145+
$persistent = isset($query['persistent']) ? filter_var($query['persistent'], FILTER_VALIDATE_BOOL) : null;
146+
$password = $query['auth'] ?? null;
147+
$database = isset($query['database']) ? (int) $query['database'] : 0;
148+
$timeout = isset($query['timeout']) ? (float) $query['timeout'] : 0.0;
149+
$prefix = $query['prefix'] ?? null;
149150

150151
$this->savePath = [
151-
'host' => $host,
152-
'port' => $port,
153-
'password' => $password,
154-
'database' => $database,
155-
'timeout' => $timeout,
152+
'host' => $host,
153+
'port' => $port,
154+
'password' => $password,
155+
'database' => $database,
156+
'timeout' => $timeout,
157+
'persistent' => $persistent,
156158
];
157159

158160
if ($prefix !== null) {
@@ -176,13 +178,11 @@ public function open($path, $name): bool
176178

177179
$redis = new Redis();
178180

179-
if (
180-
! $redis->connect(
181-
$this->savePath['host'],
182-
$this->savePath['port'],
183-
$this->savePath['timeout'],
184-
)
185-
) {
181+
$funcConnection = isset($this->savePath['persistent']) && $this->savePath['persistent'] === true
182+
? 'pconnect'
183+
: 'connect';
184+
185+
if ($redis->{$funcConnection}($this->savePath['host'], $this->savePath['port'], $this->savePath['timeout']) === false) {
186186
$this->logger->error('Session: Unable to connect to Redis with the configured settings.');
187187
} elseif (isset($this->savePath['password']) && ! $redis->auth($this->savePath['password'])) {
188188
$this->logger->error('Session: Unable to authenticate to Redis instance.');

tests/system/Session/Handlers/Database/RedisHandlerTest.php

Lines changed: 92 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -163,81 +163,133 @@ public static function provideSetSavePath(): iterable
163163
'w/o protocol' => [
164164
'127.0.0.1:6379',
165165
[
166-
'host' => 'tcp://127.0.0.1',
167-
'port' => 6379,
168-
'password' => null,
169-
'database' => 0,
170-
'timeout' => 0.0,
166+
'host' => 'tcp://127.0.0.1',
167+
'port' => 6379,
168+
'password' => null,
169+
'database' => 0,
170+
'timeout' => 0.0,
171+
'persistent' => null,
171172
],
172173
],
173174
'tls auth' => [
174175
'tls://127.0.0.1:6379?auth=password',
175176
[
176-
'host' => 'tls://127.0.0.1',
177-
'port' => 6379,
178-
'password' => 'password',
179-
'database' => 0,
180-
'timeout' => 0.0,
177+
'host' => 'tls://127.0.0.1',
178+
'port' => 6379,
179+
'password' => 'password',
180+
'database' => 0,
181+
'timeout' => 0.0,
182+
'persistent' => null,
181183
],
182184
],
183185
'tcp auth' => [
184186
'tcp://127.0.0.1:6379?auth=password',
185187
[
186-
'host' => 'tcp://127.0.0.1',
187-
'port' => 6379,
188-
'password' => 'password',
189-
'database' => 0,
190-
'timeout' => 0.0,
188+
'host' => 'tcp://127.0.0.1',
189+
'port' => 6379,
190+
'password' => 'password',
191+
'database' => 0,
192+
'timeout' => 0.0,
193+
'persistent' => null,
191194
],
192195
],
193196
'timeout float' => [
194197
'tcp://127.0.0.1:6379?timeout=2.5',
195198
[
196-
'host' => 'tcp://127.0.0.1',
197-
'port' => 6379,
198-
'password' => null,
199-
'database' => 0,
200-
'timeout' => 2.5,
199+
'host' => 'tcp://127.0.0.1',
200+
'port' => 6379,
201+
'password' => null,
202+
'database' => 0,
203+
'timeout' => 2.5,
204+
'persistent' => null,
201205
],
202206
],
203207
'timeout int' => [
204208
'tcp://127.0.0.1:6379?timeout=10',
205209
[
206-
'host' => 'tcp://127.0.0.1',
207-
'port' => 6379,
208-
'password' => null,
209-
'database' => 0,
210-
'timeout' => 10.0,
210+
'host' => 'tcp://127.0.0.1',
211+
'port' => 6379,
212+
'password' => null,
213+
'database' => 0,
214+
'timeout' => 10.0,
215+
'persistent' => null,
211216
],
212217
],
213218
'auth acl' => [
214219
'tcp://localhost:6379?auth[user]=redis-admin&auth[pass]=admin-password',
215220
[
216-
'host' => 'tcp://localhost',
217-
'port' => 6379,
218-
'password' => ['user' => 'redis-admin', 'pass' => 'admin-password'],
219-
'database' => 0,
220-
'timeout' => 0.0,
221+
'host' => 'tcp://localhost',
222+
'port' => 6379,
223+
'password' => ['user' => 'redis-admin', 'pass' => 'admin-password'],
224+
'database' => 0,
225+
'timeout' => 0.0,
226+
'persistent' => null,
221227
],
222228
],
223229
'unix domain socket' => [
224230
'unix:///tmp/redis.sock',
225231
[
226-
'host' => '/tmp/redis.sock',
227-
'port' => 0,
228-
'password' => null,
229-
'database' => 0,
230-
'timeout' => 0.0,
232+
'host' => '/tmp/redis.sock',
233+
'port' => 0,
234+
'password' => null,
235+
'database' => 0,
236+
'timeout' => 0.0,
237+
'persistent' => null,
231238
],
232239
],
233240
'unix domain socket w/o protocol' => [
234241
'/tmp/redis.sock',
235242
[
236-
'host' => '/tmp/redis.sock',
237-
'port' => 0,
238-
'password' => null,
239-
'database' => 0,
240-
'timeout' => 0.0,
243+
'host' => '/tmp/redis.sock',
244+
'port' => 0,
245+
'password' => null,
246+
'database' => 0,
247+
'timeout' => 0.0,
248+
'persistent' => null,
249+
],
250+
],
251+
'persistent connection with numeric one' => [
252+
'tcp://127.0.0.1:6379?timeout=10&persistent=1',
253+
[
254+
'host' => 'tcp://127.0.0.1',
255+
'port' => 6379,
256+
'password' => null,
257+
'database' => 0,
258+
'timeout' => 10.0,
259+
'persistent' => true,
260+
],
261+
],
262+
'no persistent connection with numeric zero' => [
263+
'tcp://127.0.0.1:6379?timeout=10&persistent=0',
264+
[
265+
'host' => 'tcp://127.0.0.1',
266+
'port' => 6379,
267+
'password' => null,
268+
'database' => 0,
269+
'timeout' => 10.0,
270+
'persistent' => false,
271+
],
272+
],
273+
'persistent connection with boolean true' => [
274+
'tcp://127.0.0.1:6379?timeout=10&persistent=true',
275+
[
276+
'host' => 'tcp://127.0.0.1',
277+
'port' => 6379,
278+
'password' => null,
279+
'database' => 0,
280+
'timeout' => 10.0,
281+
'persistent' => true,
282+
],
283+
],
284+
'persistent connection with boolean false' => [
285+
'tcp://127.0.0.1:6379?timeout=10&persistent=false',
286+
[
287+
'host' => 'tcp://127.0.0.1',
288+
'port' => 6379,
289+
'password' => null,
290+
'database' => 0,
291+
'timeout' => 10.0,
292+
'persistent' => false,
241293
],
242294
],
243295
];

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Libraries
270270
- **Image:** The ``ImageMagickHandler`` has been rewritten to rely solely on the PHP ``imagick`` extension.
271271
- **Image:** Added ``ImageMagickHandler::clearMetadata()`` method to remove image metadata for privacy protection.
272272
- **ResponseTrait:** Added ``paginate``` method to simplify paginated API responses. See :ref:`ResponseTrait::paginate() <api_response_trait_paginate>` for details.
273+
- **Session:** Added ``persistent`` config item to redis handler.
273274
- **Time:** added methods ``Time::addCalendarMonths()`` and ``Time::subCalendarMonths()``
274275
- **Time:** Added ``Time::isPast()`` and ``Time::isFuture()`` convenience methods. See :ref:`isPast <time-comparing-two-times-isPast>` and :ref:`isFuture <time-comparing-two-times-isFuture>` for details.
275276
- **View:** Added the ability to override namespaced views (e.g., from modules/packages) by placing a matching file structure within the **app/Views/overrides** directory. See :ref:`Overriding Namespaced Views <views-overriding-namespaced-views>` for details.

0 commit comments

Comments
 (0)