Added cache to the search function
Signed-off-by: Valentin Popov <info@valentineus.link>
This commit is contained in:
		
							
								
								
									
										21
									
								
								lib.php
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								lib.php
									
									
									
									
									
								
							@@ -47,17 +47,25 @@ function local_webhooks_change_status($serviceid) {
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Search for services that contain the specified event.
 | 
					 * Search for services that contain the specified event.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param  string $eventname
 | 
					 * @param string $eventname
 | 
				
			||||||
 * @param  number $limitfrom
 | 
					 * @param number $limitfrom
 | 
				
			||||||
 * @param  number $limitnum
 | 
					 * @param number $limitnum
 | 
				
			||||||
 * @return array
 | 
					 * @return array
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function local_webhooks_search_record($eventname, $limitfrom = 0, $limitnum = 0) {
 | 
					function local_webhooks_search_record($eventname, $limitfrom = 0, $limitnum = 0) {
 | 
				
			||||||
    global $DB;
 | 
					    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);
 | 
					    $rs = $DB->get_recordset(LOCAL_WEBHOOKS_TABLE_EVENTS, array("name" => $eventname, "status" => true), "id", "*", $limitfrom, $limitnum);
 | 
				
			||||||
    $result = array();
 | 
					    $result = array();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Loads services */
 | 
				
			||||||
    foreach ($rs as $event) {
 | 
					    foreach ($rs as $event) {
 | 
				
			||||||
        if ($record = $DB->get_record(LOCAL_WEBHOOKS_TABLE_SERVICES, array("id" => $event->serviceid, "status" => true), "*", IGNORE_MISSING)) {
 | 
					        if ($record = $DB->get_record(LOCAL_WEBHOOKS_TABLE_SERVICES, array("id" => $event->serviceid, "status" => true), "*", IGNORE_MISSING)) {
 | 
				
			||||||
            $result[] = $record;
 | 
					            $result[] = $record;
 | 
				
			||||||
@@ -66,6 +74,9 @@ function local_webhooks_search_record($eventname, $limitfrom = 0, $limitnum = 0)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    $rs->close();
 | 
					    $rs->close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Saves the result in the cache */
 | 
				
			||||||
 | 
					    local_webhooks_cache_set($namecache, $result);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return $result;
 | 
					    return $result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -262,14 +273,14 @@ function local_webhooks_restore_backup($backup) {
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Send the event remotely to the service.
 | 
					 * Send the event remotely to the service.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param  array  $event
 | 
					 * @param  object $event
 | 
				
			||||||
 * @param  object $record
 | 
					 * @param  object $record
 | 
				
			||||||
 * @return array
 | 
					 * @return array
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function local_webhooks_send_request($event, $record) {
 | 
					function local_webhooks_send_request($event, $record) {
 | 
				
			||||||
    global $CFG;
 | 
					    global $CFG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $event["host"]  = parse_url($CFG->wwwroot)["host"];
 | 
					    $event["host"]  = parse_url($CFG->wwwroot)["host"]; /* @todo: Fucking shit */
 | 
				
			||||||
    $event["token"] = $record->token;
 | 
					    $event["token"] = $record->token;
 | 
				
			||||||
    $event["extra"] = $record->other;
 | 
					    $event["extra"] = $record->other;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user