From eb1372a8a77bb4552995b4fe195ef75ca34556ba Mon Sep 17 00:00:00 2001 From: Qihui Chan Date: Fri, 13 May 2022 13:48:28 +1000 Subject: [PATCH] Issue#209 Remove as much as db calls possible --- classes/dml/outagecache.php | 85 +++++++++++++++++++++++++++++++++++++ classes/dml/outagedb.php | 43 ++++++++++++------- db/caches.php | 35 +++++++++++++++ lang/en/auth_outage.php | 10 +++++ version.php | 4 +- 5 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 classes/dml/outagecache.php create mode 100644 db/caches.php diff --git a/classes/dml/outagecache.php b/classes/dml/outagecache.php new file mode 100644 index 0000000..62a7585 --- /dev/null +++ b/classes/dml/outagecache.php @@ -0,0 +1,85 @@ +. + + + +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 + * @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'); + } +} \ No newline at end of file diff --git a/classes/dml/outagedb.php b/classes/dml/outagedb.php index 963ac3f..b7b0b35 100644 --- a/classes/dml/outagedb.php +++ b/classes/dml/outagedb.php @@ -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; } } diff --git a/db/caches.php b/db/caches.php new file mode 100644 index 0000000..6a14a7b --- /dev/null +++ b/db/caches.php @@ -0,0 +1,35 @@ +. + +/** + * Define cache for plugin. + * + * @package auth_outage + * @author Qihui Chan + * @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, + ], +]; \ No newline at end of file diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 0324a19..7727311 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -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.'; \ No newline at end of file diff --git a/version.php b/version.php index 304b90b..ca2033e 100644 --- a/version.php +++ b/version.php @@ -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.