MDL-69207 core_h5p: Add muc cache support for h5p library files
authorJordan Tomkinson <jordan.tomkinson@ethinkeducation.com>
Mon, 6 Jul 2020 07:00:37 +0000 (09:00 +0200)
committerVictor Deniz Falcon <victor@moodle.com>
Thu, 6 Aug 2020 01:40:24 +0000 (02:40 +0100)
h5p/classes/file_storage.php
h5p/upgrade.txt
lang/en/cache.php
lib/db/caches.php
version.php

index 675645c..c1e24ad 100644 (file)
@@ -561,13 +561,23 @@ class file_storage implements \H5PFileStorage {
      * @param  array $library Library details
      */
     public function delete_library(array $library): void {
+        global $DB;
 
         // A library ID of false would result in all library files being deleted, which we don't want. Return instead.
         if ($library['libraryId'] === false) {
             return;
         }
 
+        $areafiles = $this->fs->get_area_files($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']);
         $this->delete_directory($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']);
+        $librarycache = \cache::make('core', 'h5p_library_files');
+        foreach ($areafiles as $file) {
+            if (!$DB->record_exists('files', array('contenthash' => $file->get_contenthash(),
+                                                   'component' => self::COMPONENT,
+                                                   'filearea' => self::LIBRARY_FILEAREA))) {
+                $librarycache->delete($file->get_contenthash());
+            }
+        }
     }
 
     /**
@@ -590,6 +600,7 @@ class file_storage implements \H5PFileStorage {
      * @param  array  $options File system information.
      */
     private function copy_directory(string $source, array $options): void {
+        $librarycache = \cache::make('core', 'h5p_library_files');
         $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
                 \RecursiveIteratorIterator::SELF_FIRST);
 
@@ -607,7 +618,13 @@ class file_storage implements \H5PFileStorage {
                     $options['filepath'] = $root;
                 }
 
-                $this->fs->create_file_from_pathname($options, $item->getPathName());
+                $file = $this->fs->create_file_from_pathname($options, $item->getPathName());
+
+                if ($options['filearea'] == self::LIBRARY_FILEAREA) {
+                    if (!$librarycache->has($file->get_contenthash())) {
+                        $librarycache->set($file->get_contenthash(), file_get_contents($item->getPathName()));
+                    }
+                }
             }
             $it->next();
         }
@@ -629,12 +646,24 @@ class file_storage implements \H5PFileStorage {
         // Read source files.
         $files = $this->fs->get_directory_files($contextid, self::COMPONENT, $filearea, $itemid, $filepath, true);
 
+        $librarycache = \cache::make('core', 'h5p_library_files');
+
         foreach ($files as $file) {
             $path = $target . str_replace($filepath, DIRECTORY_SEPARATOR, $file->get_filepath());
             if ($file->is_directory()) {
                 check_dir_exists(rtrim($path));
             } else {
-                $file->copy_content_to($path . $file->get_filename());
+                if ($filearea == self::LIBRARY_FILEAREA) {
+                    $cachedfile = $librarycache->get($file->get_contenthash());
+                    if (empty($cachedfile)) {
+                        $file->copy_content_to($path . $file->get_filename());
+                        $librarycache->set($file->get_contenthash(), file_get_contents($path . $file->get_filename()));
+                    } else {
+                        file_put_contents($path . $file->get_filename(), $cachedfile);
+                    }
+                } else {
+                    $file->copy_content_to($path . $file->get_filename());
+                }
             }
         }
     }
index 0031d7d..59eb0cb 100644 (file)
@@ -1,9 +1,12 @@
 This files describes API changes in core libraries and APIs,
 information provided here is intended especially for developers.
 
+=== 4.0 ===
+* Added a new cache for h5p_library_files (MDL-69207)
+
 === 3.9 ===
 * A new plugintype has been created, h5plib, for having installed more
 than one H5P library version.
 * H5P third-party libraries have been moved from /lib/h5p to h5p/h5plib/v124,
 as an h5plib plugintype.
-* H5P Editor PHP library added to h5plib v124 plugin.
\ No newline at end of file
+* H5P Editor PHP library added to h5plib v124 plugin.
index 8ca427a..5a374dd 100644 (file)
@@ -58,6 +58,7 @@ $string['cachedef_fontawesomeiconmapping'] = 'Mapping of icons for font awesome'
 $string['cachedef_suspended_userids'] = 'List of suspended users per course';
 $string['cachedef_groupdata'] = 'Course group information';
 $string['cachedef_h5p_content_type_translations'] = 'H5P content-type libraries translations';
+$string['cachedef_h5p_library_files'] = 'H5P library files';
 $string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
 $string['cachedef_langmenu'] = 'List of available languages';
 $string['cachedef_license'] = 'List of licences';
index 14dca04..66dbe76 100644 (file)
@@ -455,6 +455,13 @@ $definitions = array(
         'simpledata' => true,
     ],
 
+    // File cache for H5P Library files.
+    'h5p_library_files' => [
+        'mode' => cache_store::MODE_APPLICATION,
+        'simplekeys' => true,
+        'canuselocalstore' => true
+    ],
+
     // Cache the grade letters for faster retrival.
     'grade_letters' => [
         'mode'                   => cache_store::MODE_REQUEST,
index 5ea5e1f..7e9b1e0 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020073000.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2020073000.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '4.0dev (Build: 20200730)'; // Human-friendly version name