aboutsummaryrefslogtreecommitdiff
path: root/lib.php
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2018-03-20 02:58:45 +0300
committerValentin Popov <info@valentineus.link>2018-03-20 02:58:45 +0300
commit279fca5ed18cdbd12df0957c62f2657e72e34f4e (patch)
tree4d0839a3d8c6d7ce565c73fd24a045e3a33a0568 /lib.php
parent5036ef261c46d9759d600769b7d1000f43155ef0 (diff)
downloadlocal_webhooks-279fca5ed18cdbd12df0957c62f2657e72e34f4e.tar.xz
local_webhooks-279fca5ed18cdbd12df0957c62f2657e72e34f4e.zip
Added cache to the search function
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'lib.php')
-rw-r--r--lib.php21
1 files changed, 16 insertions, 5 deletions
diff --git a/lib.php b/lib.php
index 69b2f00..68750bd 100644
--- a/lib.php
+++ b/lib.php
@@ -47,17 +47,25 @@ function local_webhooks_change_status($serviceid) {
/**
* Search for services that contain the specified event.
*
- * @param string $eventname
- * @param number $limitfrom
- * @param number $limitnum
+ * @param string $eventname
+ * @param number $limitfrom
+ * @param number $limitnum
* @return array
*/
function local_webhooks_search_record($eventname, $limitfrom = 0, $limitnum = 0) {
global $DB;
+ /* Checks for the presence of a cache */
+ $namecache = "${eventname}_${limitfrom}_${limitnum}";
+ if (is_array($records = local_webhooks_cache_get($namecache))) {
+ return $records;
+ }
+
+ /* Loads a list of events */
$rs = $DB->get_recordset(LOCAL_WEBHOOKS_TABLE_EVENTS, array("name" => $eventname, "status" => true), "id", "*", $limitfrom, $limitnum);
$result = array();
+ /* Loads services */
foreach ($rs as $event) {
if ($record = $DB->get_record(LOCAL_WEBHOOKS_TABLE_SERVICES, array("id" => $event->serviceid, "status" => true), "*", IGNORE_MISSING)) {
$result[] = $record;
@@ -66,6 +74,9 @@ function local_webhooks_search_record($eventname, $limitfrom = 0, $limitnum = 0)
$rs->close();
+ /* Saves the result in the cache */
+ local_webhooks_cache_set($namecache, $result);
+
return $result;
}
@@ -262,14 +273,14 @@ function local_webhooks_restore_backup($backup) {
/**
* Send the event remotely to the service.
*
- * @param array $event
+ * @param object $event
* @param object $record
* @return array
*/
function local_webhooks_send_request($event, $record) {
global $CFG;
- $event["host"] = parse_url($CFG->wwwroot)["host"];
+ $event["host"] = parse_url($CFG->wwwroot)["host"]; /* @todo: Fucking shit */
$event["token"] = $record->token;
$event["extra"] = $record->other;