diff --git a/lang/en/cache.php b/lang/en/cache.php index 771393b21e0..ba74bfeafc1 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -61,6 +61,7 @@ $string['cachedef_databasemeta'] = 'Database meta information'; $string['cachedef_eventinvalidation'] = 'Event invalidation'; $string['cachedef_externalbadges'] = 'External badges for particular user'; $string['cachedef_fontawesomeiconmapping'] = 'Mapping of icons for font awesome'; +$string['cachedef_file_imageinfo'] = 'File image info eg dimensions'; $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'; diff --git a/lib/db/caches.php b/lib/db/caches.php index ab449d750db..d991e8418e1 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -557,4 +557,14 @@ $definitions = array( 'staticacceleration' => true, 'ttl' => 1800, ], + + // Cache image dimensions. + 'file_imageinfo' => [ + 'mode' => cache_store::MODE_APPLICATION, + 'simplekeys' => true, + 'simpledata' => true, + 'staticacceleration' => true, + 'canuselocalstore' => true, + 'staticaccelerationsize' => 100, + ], ); diff --git a/lib/filestorage/file_system.php b/lib/filestorage/file_system.php index b196c824efd..0f9c726add0 100644 --- a/lib/filestorage/file_system.php +++ b/lib/filestorage/file_system.php @@ -365,9 +365,18 @@ abstract class file_system { return false; } + $hash = $file->get_contenthash(); + $cache = cache::make('core', 'file_imageinfo'); + $info = $cache->get($hash); + if ($info !== false) { + return $info; + } + // Whilst get_imageinfo_from_path can use remote paths, it must download the entire file first. // It is more efficient to use a local file when possible. - return $this->get_imageinfo_from_path($this->get_local_path_from_storedfile($file, true)); + $info = $this->get_imageinfo_from_path($this->get_local_path_from_storedfile($file, true)); + $cache->set($hash, $info); + return $info; } /**