From 6eb9e0387236acf71d7a1ecb722511aa056fef2a Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 12 Jun 2015 00:54:46 +0200 Subject: [PATCH] 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. --- repository/youtube/lib.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/repository/youtube/lib.php b/repository/youtube/lib.php index 51a756feea2..afe8f2b8094 100644 --- a/repository/youtube/lib.php +++ b/repository/youtube/lib.php @@ -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,