From 5de03d6b9fcc7a6e1167baeba92c4416516320bb Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 18 Feb 2017 12:54:26 +0100 Subject: [PATCH] [FileCache] Use serialize instead of json_encode json_encode causes high memory footprint on large input data, where serialize is less problematic. Example: When using AcrimedBridge items contain pictures in raw format (entire picture) which leads to a file size of about 2MB using serialize. json_encode will allocate about 98MB of memory for encoding, causing memory exhausion errors (PHP allows for 128MB of memory by default) --- caches/FileCache.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 67078eb..59bced9 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -8,11 +8,13 @@ class FileCache implements CacheInterface { protected $param; public function loadData(){ - return json_decode(file_get_contents($this->getCacheFile()), true); + return unserialize(file_get_contents($this->getCacheFile())); } public function saveData($datas){ - $writeStream = file_put_contents($this->getCacheFile(), json_encode($datas, JSON_PRETTY_PRINT)); + // Notice: We use plain serialize() here to reduce memory footprint on + // large input data. + $writeStream = file_put_contents($this->getCacheFile(), serialize($datas)); if($writeStream === false) { throw new \Exception("Cannot write the cache... Do you have the right permissions ?"); @@ -110,6 +112,8 @@ class FileCache implements CacheInterface { throw new \Exception('Call "setParameters" first!'); } - return hash('md5', http_build_query($this->param)) . '.cache'; + // Change character when making incompatible changes to prevent loading + // errors due to incompatible file contents \|/ + return hash('md5', http_build_query($this->param) . 'A') . '.cache'; } }