From 528dbaf7bb08717b02c579a06d2caf2d29e72b89 Mon Sep 17 00:00:00 2001 From: Joshua Dickerson Date: Fri, 23 Dec 2016 15:01:32 -0500 Subject: [PATCH 1/3] Use a real PHP array for the cache This creates an array instead of using a serialized object for the cache. This should be more efficient. --- app/code/community/Varien/Autoload.php | 71 +++++++++++++++++++------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/app/code/community/Varien/Autoload.php b/app/code/community/Varien/Autoload.php index 3b84e4c..5d9c977 100644 --- a/app/code/community/Varien/Autoload.php +++ b/app/code/community/Varien/Autoload.php @@ -178,11 +178,11 @@ public static function loadCacheContent() self::setCache($value); } } elseif (file_exists(self::getCacheFilePath())) { - $cacheFilePath = unserialize(file_get_contents(self::getCacheFilePath())); - // If the file is corrupted, it resets cache - if ($cacheFilePath === false) { + $cacheFilePath = require self::getCacheFilePath(); + if (!is_array($cacheFilePath)) { $cacheFilePath = array(); } + self::setCache($cacheFilePath); } @@ -266,27 +266,62 @@ public static function getCache() return self::$_cache; } + /** + * Save the cache to disk + */ + public function save() + { + if (self::isApcUsed()) { + if (PHP_SAPI != 'cli') { + apc_store(self::getCacheKey(), self::$_cache, 0); + } + } elseif (is_dir_writeable(dirname(self::getCacheFilePath()))) { + $this->_createCacheFile(); + } + } + + /** + * Create the cache file + */ + protected function _createCacheFile() + { + $content = " $filename) { + $content .= "\t'$key' => '$filename',\n"; + } + + $content .= '];'; + + return $this->_writeCacheFile($content); + } + + /** + * Write the cache to disk + * + * @param string $content + * @return int + */ + protected function _writeCacheFile($content) + { + $tmpFile = tempnam(sys_get_temp_dir(), 'aoe_classpathcache'); + if (file_put_contents($tmpFile, $content)) { + if (@rename($tmpFile, self::getCacheFilePath())) { + @chmod(self::getCacheFilePath(), 0664); + } else { + @unlink($tmpFile); + } + } + } + /** * Class destructor */ public function __destruct() { if (self::$_numberOfFilesAddedToCache > 0) { - if (self::isApcUsed()) { - if (PHP_SAPI != 'cli') { - apc_store(self::getCacheKey(), self::$_cache, 0); - } - } elseif (is_dir_writeable(dirname(self::getCacheFilePath()))) { - $fileContent = serialize(self::$_cache); - $tmpFile = tempnam(sys_get_temp_dir(), 'aoe_classpathcache'); - if (file_put_contents($tmpFile, $fileContent)) { - if (@rename($tmpFile, self::getCacheFilePath())) { - @chmod(self::getCacheFilePath(), 0664); - } else { - @unlink($tmpFile); - } - } - } + $this->save(); } } } From d79225f96a34abccc6f2311de9b672ce2b7b13a5 Mon Sep 17 00:00:00 2001 From: Joshua Dickerson Date: Fri, 23 Dec 2016 15:12:53 -0500 Subject: [PATCH 2/3] Bad docblock --- app/code/community/Varien/Autoload.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/community/Varien/Autoload.php b/app/code/community/Varien/Autoload.php index 5d9c977..b8e6c9f 100644 --- a/app/code/community/Varien/Autoload.php +++ b/app/code/community/Varien/Autoload.php @@ -294,14 +294,13 @@ protected function _createCacheFile() $content .= '];'; - return $this->_writeCacheFile($content); + $this->_writeCacheFile($content); } /** * Write the cache to disk * * @param string $content - * @return int */ protected function _writeCacheFile($content) { From 2363f451d8b65a4e6cae5d58ee1cb1b5696222de Mon Sep 17 00:00:00 2001 From: Joshua Dickerson Date: Fri, 23 Dec 2016 15:14:40 -0500 Subject: [PATCH 3/3] Support older PHP versions I didn't realize I had to support PHP 5.3 --- app/code/community/Varien/Autoload.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Varien/Autoload.php b/app/code/community/Varien/Autoload.php index b8e6c9f..f0674d3 100644 --- a/app/code/community/Varien/Autoload.php +++ b/app/code/community/Varien/Autoload.php @@ -285,14 +285,14 @@ public function save() */ protected function _createCacheFile() { - $content = " $filename) { $content .= "\t'$key' => '$filename',\n"; } - $content .= '];'; + $content .= ');'; $this->_writeCacheFile($content); }