mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Issue#209 Remove as much as db calls possible
This commit is contained in:
85
classes/dml/outagecache.php
Normal file
85
classes/dml/outagecache.php
Normal 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');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user