diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-03-17 12:35:47 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-03-17 12:35:47 +0100 |
commit | 9e2ebf2dcea8d3e1ee153eba43124ca95c496443 (patch) | |
tree | d48d32dcd87aa77304c7dd9f0acc6d79fd2993fd /lib/private/urlgenerator.php | |
parent | d96b97043b2fa7ba4d676c1d7b44f4aa5e58c8ee (diff) | |
download | nextcloud-server-9e2ebf2dcea8d3e1ee153eba43124ca95c496443.tar.gz nextcloud-server-9e2ebf2dcea8d3e1ee153eba43124ca95c496443.zip |
Cache \OC\URLGenerator::imagePath
\OC\URLGenerator::imagePath is a really expensive operation due to all the I/O handling and can really benefit from caching.
Diffstat (limited to 'lib/private/urlgenerator.php')
-rw-r--r-- | lib/private/urlgenerator.php | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/lib/private/urlgenerator.php b/lib/private/urlgenerator.php index e87a6c354fb..8e65858547c 100644 --- a/lib/private/urlgenerator.php +++ b/lib/private/urlgenerator.php @@ -9,6 +9,8 @@ namespace OC; use OC_Defaults; +use OCP\ICacheFactory; +use OCP\IConfig; use OCP\IURLGenerator; use RuntimeException; @@ -16,17 +18,19 @@ use RuntimeException; * Class to generate URLs */ class URLGenerator implements IURLGenerator { - - /** - * @var \OCP\IConfig - */ + /** @var IConfig */ private $config; + /** @var ICacheFactory */ + private $cacheFactory; /** - * @param \OCP\IConfig $config + * @param IConfig $config + * @param ICacheFactory $cacheFactory */ - public function __construct($config) { + public function __construct(IConfig $config, + ICacheFactory $cacheFactory) { $this->config = $config; + $this->cacheFactory = $cacheFactory; } /** @@ -116,37 +120,49 @@ class URLGenerator implements IURLGenerator { // Read the selected theme from the config file $theme = \OC_Util::getTheme(); + $cache = $this->cacheFactory->create('imagePath'); + $cacheKey = $app.'-'.$image; + if($cache->hasKey($cacheKey)) { + return $cache->get($cacheKey); + } + //if a theme has a png but not an svg always use the png $basename = substr(basename($image),0,-4); // Check if the app is in the app folder + $path = ''; if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) { - return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image"; + $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image"; } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg") && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) { - return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png"; + $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png"; } elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) { - return \OC_App::getAppWebPath($app) . "/img/$image"; + $path = \OC_App::getAppWebPath($app) . "/img/$image"; } elseif (!file_exists(\OC_App::getAppPath($app) . "/img/$basename.svg") && file_exists(\OC_App::getAppPath($app) . "/img/$basename.png")) { - return \OC_App::getAppPath($app) . "/img/$basename.png"; + $path = \OC_App::getAppPath($app) . "/img/$basename.png"; } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) { - return \OC::$WEBROOT . "/themes/$theme/$app/img/$image"; + $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$image"; } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg") && file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) { - return \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png"; + $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png"; } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) { - return \OC::$WEBROOT . "/$app/img/$image"; + $path = \OC::$WEBROOT . "/$app/img/$image"; } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg") && file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) { - return \OC::$WEBROOT . "/$app/img/$basename.png"; + $path = \OC::$WEBROOT . "/$app/img/$basename.png"; } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) { - return \OC::$WEBROOT . "/themes/$theme/core/img/$image"; + $path = \OC::$WEBROOT . "/themes/$theme/core/img/$image"; } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { - return \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; + $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) { - return \OC::$WEBROOT . "/core/img/$image"; + $path = \OC::$WEBROOT . "/core/img/$image"; + } + + if($path !== '') { + $cache->set($cacheKey, $path); + return $path; } else { throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); } |