Issue#209 Remove as much as db calls possible

This commit is contained in:
Qihui Chan
2022-05-13 13:48:28 +10:00
parent 0344500671
commit eb1372a8a7
5 changed files with 159 additions and 18 deletions

View File

@@ -0,0 +1,85 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage\dml;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/calendar/lib.php');
/**
* outagecache class.
*
* To manipulate outage cache.
*
* @package auth_outage
* @author Qihui Chan <qihuichan@catalyst-au.net>
* @copyright 2022 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagecache{
/**
* Private constructor, use static methods instead.
*/
private function __construct() {
}
/**
* Set active outage cache.
*
* @param outage|null $next_outage Next outage to save.
*/
public static function set_active_outage_cache($next_outage = null) {
$cache = \cache::make('auth_outage', 'cache_active_outage_data');
$cache->set('cache_active_outage_data', $next_outage);
}
/**
* Get active outage cache.
*/
public static function get_active_outage_cache() {
$cache = \cache::make('auth_outage', 'cache_active_outage_data');
return $cache->get('cache_active_outage_data');
}
/**
* Set onging outage cache.
*
* @param outage|null $next_outage Onging outage to save.
*/
public static function set_ongoing_outage_cache($onging_outage = null) {
$cache = \cache::make('auth_outage', 'cache_ongoing_outage_data');
$cache->set('cache_ongoing_outage_data', $onging_outage);
}
/**
* Get onging outage cache.
*/
public static function get_ongoing_outage_cache() {
$cache = \cache::make('auth_outage', 'cache_ongoing_outage_data');
return $cache->get('cache_ongoing_outage_data');
}
/**
* Delete onging outage cache.
*/
public static function delete_ongoing_outage_cache() {
$cache = \cache::make('auth_outage', 'cache_ongoing_outage_data');
$cache->delete('cache_ongoing_outage_data');
}
}

View File

@@ -34,6 +34,7 @@ use auth_outage\event\outage_deleted;
use auth_outage\event\outage_updated;
use auth_outage\local\outage;
use auth_outage\local\outagelib;
use auth_outage\dml\outagecache;
use coding_exception;
defined('MOODLE_INTERNAL') || die();
@@ -149,6 +150,9 @@ class outagedb {
calendar::update($outage);
}
// Set the next starting outage to cache after an outage is saved in db.
outagecache::set_active_outage_cache(self::get_next_starting());
// Trigger outages modified events.
outagelib::prepare_next_outage(true);
@@ -188,6 +192,9 @@ class outagedb {
$DB->delete_records('auth_outage', ['id' => $id]);
calendar::delete($id);
// Set the next starting outage to cache after an outage is deleted in db.
outagecache::set_active_outage_cache(self::get_next_starting());
// Trigger events.
outagelib::prepare_next_outage();
}
@@ -211,21 +218,9 @@ class outagedb {
throw new coding_exception('$time must be null or a positive int.', $time);
}
$select = ':datetime2 <= stoptime AND (finished IS NULL OR :datetime3 <= finished)'; // End condition.
$select = "(warntime <= :datetime1 AND (${select}))"; // Full select part.
$data = $DB->get_records_select(
'auth_outage',
$select,
['datetime1' => $time, 'datetime2' => $time, 'datetime3' => $time],
'starttime ASC, stoptime DESC, title ASC',
'*',
0,
1
);
// Not using $DB->get_record_select instead because there is no 'limit' parameter.
// Allowing multiple records still raises an internal error.
return (count($data) == 0) ? null : new outage(array_shift($data));
$outage_cache = outagecache::get_active_outage_cache();
return $outage_cache && $outage_cache->warntime <= $time && $outage_cache->stoptime >= $time
? new outage($outage_cache) : null;
}
/**
@@ -318,6 +313,9 @@ class outagedb {
}
$outage->finished = $time;
outagecache::delete_ongoing_outage_cache();
self::save($outage);
}
@@ -399,6 +397,13 @@ class outagedb {
throw new coding_exception('$time must be null or a positive int.', $time);
}
// If there is an onging outage in cache, return the outage.
$outage_cache = outagecache::get_ongoing_outage_cache();
if ($outage_cache) {
return $outage_cache->starttime <= $time && $outage_cache->stoptime >= $time
? new outage($outage_cache) : null;
}
$data = $DB->get_records_select(
'auth_outage',
'starttime <= :datetime1 AND :datetime2 <= stoptime AND finished IS NULL',
@@ -411,6 +416,12 @@ class outagedb {
// Not using $DB->get_record_select instead because there is no 'limit' parameter.
// Allowing multiple records still raises an internal error.
return (count($data) == 0) ? null : new outage(array_shift($data));
if (count($data)) {
$outage = new outage(array_shift($data));
outagecache::set_ongoing_outage_cache($outage);
return $outage;
}
return null;
}
}

35
db/caches.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Define cache for plugin.
*
* @package auth_outage
* @author Qihui Chan <qihuichan@catalyst-au.net>
* @copyright 2022 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$definitions = [
'cache_active_outage_data' => [
'mode' => cache_store::MODE_APPLICATION,
],
'cache_ongoing_outage_data' => [
'mode' => cache_store::MODE_APPLICATION,
],
];

View File

@@ -165,3 +165,13 @@ $string['warningreenablemaintenancemode'] = 'Please note that saving this outage
* Privacy provider (GDPR)
*/
$string["privacy:no_data_reason"] = "The Outage authentication plugin does not store any personal data.";
/*
* Active outage cache definition.
*/
$string['cachedef_cache_active_outage_data'] = 'Cache active outage.';
/*
* Ongoing outage cache definition.
*/
$string['cachedef_cache_ongoing_outage_data'] = 'Cache onging outage.';

View File

@@ -28,8 +28,8 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = "auth_outage";
$plugin->version = 2022042800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2022042800; // Human-readable release information.
$plugin->version = 2022120500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2022120500; // Human-readable release information.
$plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher.
$plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!
$plugin->supported = [39, 311]; // A range of branch numbers of supported moodle versions.