Skip to content

Commit 3b06b75

Browse files
author
Mr Bot
committed
Merge pull request #4855 in DEV/moodle from feature/INT-16334_excitable-donkey to master
* commit '5239ce6517b10492bb4a1bf9f2ed1ac2e32da423': INT-16334: Make methods signature compatible with PHPUnit 8.5 in Moodle 3.10 INT-16334: feat: add port & auth to redis connection
2 parents 0b7033a + c7f6474 commit 3b06b75

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ Provides a Moodle lock factory class for locking with Redis. This plugin was con
99
* PHP Redis extension
1010

1111
## Installation
12-
Clone the repository or download and extract the code into the local directory of your Moodle install (e.g. $CFG->wwwroot/local/redislock) and run the site's upgrade script. Set $CFG->local_redislock_redis_server with your Redis server's connection string. Set $CFG->lock_factory to '\\\\local_redislock\\\\lock\\\\redis_lock_factory' in your config file.
12+
Clone the repository or download and extract the code into the local directory of your Moodle install (e.g. $CFG->wwwroot/local/redislock) and run the site's upgrade script.
13+
Set:
14+
* `$CFG->local_redislock_redis_server` with your Redis server's connection string.
15+
- It can be the `hostname` or IP address of the Redis server.
16+
- It can also be `hostname:port` if you want to use other port different than `6379` (Default)
17+
* `$CFG->lock_factory` to `'\\local_redislock\\lock\\redis_lock_factory'` in your config file.
18+
* `$CFG->local_redislock_auth` with your Redis server's password string.
1319

1420
## Flags
1521

classes/lock/redis_lock_factory.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class redis_lock_factory implements lock_factory {
7676
*/
7777
private static $conncount = 0;
7878

79+
/**
80+
* @var string|null Redis password.
81+
*/
82+
private $auth;
83+
7984
/**
8085
* @param string $type The type this lock is used for (e.g. cron, cache).
8186
* @param \Redis|null $redis An instance of the PHPRedis extension class.
@@ -88,6 +93,7 @@ public function __construct($type, \Redis $redis = null, $logging = null) {
8893
$this->type = $type;
8994
$this->redisserver = $CFG->local_redislock_redis_server ?? null;
9095
$this->shareconnection = empty($CFG->local_redislock_disable_shared_connection);
96+
$this->auth = $CFG->local_redislock_redis_auth ?? null;
9197
if (is_null($redis)) {
9298
shared_redis_connection::get_instance()->add_factory();
9399
$redis = $this->bootstrap_redis();
@@ -357,8 +363,19 @@ protected function bootstrap_redis() {
357363
}
358364

359365
try {
366+
// Default port.
367+
$port = 6379;
368+
$server = $this->redisserver;
369+
if (strpos($this->redisserver, ':')) {
370+
$serverconf = explode(':', $this->redisserver);
371+
$server = $serverconf[0];
372+
$port = $serverconf[1];
373+
}
360374
$redis = new \Redis();
361-
$redis->connect($this->redisserver);
375+
$redis->connect($server, $port);
376+
if (!empty($this->auth)) {
377+
$redis->auth($this->auth);
378+
}
362379
if ($this->shareconnection) {
363380
shared_redis_connection::get_instance()->set_redis($redis); // Reusing the connection.
364381
} else {

tests/redis_lock_factory_test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
class local_redislock_redis_lock_factory_test extends \advanced_testcase {
4141

42-
public function setUp() {
42+
public function setUp(): void {
4343
global $CFG;
4444

4545
$this->resetAfterTest();
@@ -52,7 +52,7 @@ public function setUp() {
5252
/**
5353
* @throws coding_exception
5454
*/
55-
protected function tearDown() {
55+
protected function tearDown(): void {
5656
shared_redis_connection::get_instance()->close();
5757
while (!empty(shared_redis_connection::get_instance()->get_factory_count())) {
5858
shared_redis_connection::get_instance()->remove_factory();

0 commit comments

Comments
 (0)