Skip to content

Commit 2609108

Browse files
ddevsrmichalsnpaulbalandan
authored
feat: added async & persistent options to Cache Redis (#9792)
* feat: added async & persistent options to cache predis * docs: update sample config * refactor: phpdocs on cache * feat: redis can used persistent * docs: added support persistent redis option * run cs-fixer * docs: notes async option only used by Predis * Update app/Config/Cache.php Co-authored-by: Michal Sniatala <michal@sniatala.pl> * Update user_guide_src/source/changelogs/v4.7.0.rst Co-authored-by: Michal Sniatala <michal@sniatala.pl> * Update user_guide_src/source/libraries/caching/014.php Co-authored-by: Michal Sniatala <michal@sniatala.pl> * Update app/Config/Cache.php Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> --------- Co-authored-by: Michal Sniatala <michal@sniatala.pl> Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent a84634d commit 2609108

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

app/Config/Cache.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,24 @@ class Cache extends BaseConfig
112112
* Your Redis server can be specified below, if you are using
113113
* the Redis or Predis drivers.
114114
*
115-
* @var array{host?: string, password?: string|null, port?: int, timeout?: int, database?: int}
115+
* @var array{
116+
* host?: string,
117+
* password?: string|null,
118+
* port?: int,
119+
* timeout?: int,
120+
* async?: bool,
121+
* persistent?: bool,
122+
* database?: int
123+
* }
116124
*/
117125
public array $redis = [
118-
'host' => '127.0.0.1',
119-
'password' => null,
120-
'port' => 6379,
121-
'timeout' => 0,
122-
'database' => 0,
126+
'host' => '127.0.0.1',
127+
'password' => null,
128+
'port' => 6379,
129+
'timeout' => 0,
130+
'async' => false, // specific to Predis and ignored by the native Redis extension
131+
'persistent' => false,
132+
'database' => 0,
123133
];
124134

125135
/**

system/Cache/Handlers/PredisHandler.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ class PredisHandler extends BaseHandler
3636
* host: string,
3737
* password: string|null,
3838
* port: int,
39+
* async: bool,
40+
* persistent: bool,
3941
* timeout: int
4042
* }
4143
*/
4244
protected $config = [
43-
'scheme' => 'tcp',
44-
'host' => '127.0.0.1',
45-
'password' => null,
46-
'port' => 6379,
47-
'timeout' => 0,
45+
'scheme' => 'tcp',
46+
'host' => '127.0.0.1',
47+
'password' => null,
48+
'port' => 6379,
49+
'async' => false,
50+
'persistent' => false,
51+
'timeout' => 0,
4852
];
4953

5054
/**

system/Cache/Handlers/RedisHandler.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class RedisHandler extends BaseHandler
3434
* password: string|null,
3535
* port: int,
3636
* timeout: int,
37+
* persistent: bool,
3738
* database: int,
3839
* }
3940
*/
4041
protected $config = [
41-
'host' => '127.0.0.1',
42-
'password' => null,
43-
'port' => 6379,
44-
'timeout' => 0,
45-
'database' => 0,
42+
'host' => '127.0.0.1',
43+
'password' => null,
44+
'port' => 6379,
45+
'timeout' => 0,
46+
'persistent' => false,
47+
'database' => 0,
4648
];
4749

4850
/**
@@ -82,10 +84,11 @@ public function initialize()
8284
$this->redis = new Redis();
8385

8486
try {
87+
$funcConnection = isset($config['persistent']) && $config['persistent'] ? 'pconnect' : 'connect';
88+
8589
// Note:: If Redis is your primary cache choice, and it is "offline", every page load will end up been delayed by the timeout duration.
8690
// I feel like some sort of temporary flag should be set, to indicate that we think Redis is "offline", allowing us to bypass the timeout for a set period of time.
87-
88-
if (! $this->redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout'])) {
91+
if (! $this->redis->{$funcConnection}($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout'])) {
8992
// Note:: I'm unsure if log_message() is necessary, however I'm not 100% comfortable removing it.
9093
log_message('error', 'Cache: Redis connection failed. Check your configuration.');
9194

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Libraries
7575

7676
- **API Transformers:** This new feature provides a structured way to transform data for API responses. See :ref:`API Transformers <api_transformers>` for details.
7777
- **CLI:** Added ``SignalTrait`` to provide unified handling of operating system signals in CLI commands.
78+
- **Cache:** Added ``async`` and ``persistent`` config item to Predis handler.
79+
- **Cache:** Added ``persistent`` config item to Redis handler.
7880
- **CURLRequest:** Added ``shareConnection`` config item to change default share connection.
7981
- **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout.
8082
- **CURLRequest:** Added ``fresh_connect`` options to enable/disable request fresh connection.

user_guide_src/source/libraries/caching/014.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ class Cache extends BaseConfig
99
// ...
1010

1111
public $redis = [
12-
'host' => '127.0.0.1',
13-
'password' => null,
14-
'port' => 6379,
15-
'timeout' => 0,
16-
'database' => 0,
12+
'host' => '127.0.0.1',
13+
'password' => null,
14+
'port' => 6379,
15+
'async' => false, // specific to Predis and ignored by the native Redis extension
16+
'persistent' => false,
17+
'timeout' => 0,
18+
'database' => 0,
1719
];
1820

1921
// ...

0 commit comments

Comments
 (0)