[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)
This commit is contained in:
logmanoriginal 2017-02-18 12:54:26 +01:00
parent 1d26c7f1c3
commit 5de03d6b9f
1 changed files with 7 additions and 3 deletions

View File

@ -8,11 +8,13 @@ class FileCache implements CacheInterface {
protected $param; protected $param;
public function loadData(){ public function loadData(){
return json_decode(file_get_contents($this->getCacheFile()), true); return unserialize(file_get_contents($this->getCacheFile()));
} }
public function saveData($datas){ 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) { if($writeStream === false) {
throw new \Exception("Cannot write the cache... Do you have the right permissions ?"); 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!'); 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';
} }
} }