mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-50176 repository_youtube: delay loading google service stuff
Loading Google client services is a little monster, eating around 2MB (opcache enabled) of memory. As far as we instantiate the repository instances really early, no matter they are not used later (editor, file picker, admin...).. we are delaying the load of the service until we know we are going to use it. Surely applying this very same (sort of lazy load) techinque to other repositories could lead to a nice memory reduction in lots of pages. MDL-50176 repository_youtube: also delay the inclusion of client. While memory was fixed with previous commit, still some extra included files where being reported, so go crazy and move all the stuff to the new init delayed method.
This commit is contained in:
parent
6bd5ca3db0
commit
6eb9e03872
@ -23,7 +23,6 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
require_once($CFG->dirroot . '/repository/lib.php');
|
||||
require_once($CFG->libdir . '/google/lib.php');
|
||||
|
||||
/**
|
||||
* repository_youtube class
|
||||
@ -66,10 +65,6 @@ class repository_youtube extends repository {
|
||||
parent::__construct($repositoryid, $context, $options);
|
||||
|
||||
$this->apikey = $this->get_option('apikey');
|
||||
$this->client = get_google_client();
|
||||
$this->client->setDeveloperKey($this->apikey);
|
||||
$this->client->setScopes(array(Google_Service_YouTube::YOUTUBE_READONLY));
|
||||
$this->service = new Google_Service_YouTube($this->client);
|
||||
|
||||
// Without an API key, don't show this repo to users as its useless without it.
|
||||
if (empty($this->apikey)) {
|
||||
@ -77,6 +72,26 @@ class repository_youtube extends repository {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Init all the youtube client service stuff.
|
||||
*
|
||||
* Instead of instantiating the service in the constructor, we delay
|
||||
* it until really neeed because it's really memory hungry (2MB). That
|
||||
* way the editor or any other artifact requiring repository instantiation
|
||||
* can do it in a cheap way. Sort of lazy loading the plugin.
|
||||
*/
|
||||
private function init_youtube_service() {
|
||||
global $CFG;
|
||||
|
||||
if (!isset($this->service)) {
|
||||
require_once($CFG->libdir . '/google/lib.php');
|
||||
$this->client = get_google_client();
|
||||
$this->client->setDeveloperKey($this->apikey);
|
||||
$this->client->setScopes(array(Google_Service_YouTube::YOUTUBE_READONLY));
|
||||
$this->service = new Google_Service_YouTube($this->client);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save apikey in config table.
|
||||
* @param array $options
|
||||
@ -176,6 +191,7 @@ class repository_youtube extends repository {
|
||||
$list = array();
|
||||
$error = null;
|
||||
try {
|
||||
$this->init_youtube_service(); // About to use the service, ensure it's loaded.
|
||||
$response = $this->service->search->listSearch('id,snippet', array(
|
||||
'q' => $keyword,
|
||||
'maxResults' => $max,
|
||||
|
Loading…
x
Reference in New Issue
Block a user