From 85c8c02a3055655631c01d16a27cbaf951d1835c Mon Sep 17 00:00:00 2001 From: Paul Dixon Date: Wed, 12 Jun 2013 17:08:40 +0100 Subject: [PATCH] optimized FileCache by retaining the most recent fetch in memory - this increases the performance of LifetimeFileCache which previously would perform two unserialize operations, one during contains() and then a second during fetch() --- Cache/FileCache.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Cache/FileCache.php b/Cache/FileCache.php index f92797e..172a6fe 100644 --- a/Cache/FileCache.php +++ b/Cache/FileCache.php @@ -32,6 +32,13 @@ class FileCache extends AbstractCache /** @var string $separator */ private $_separator = '--s--'; + /** @var string last id fetched */ + private $_lastId = null; + + /** @var string last data fetched */ + private $_lastData = null; + + /** * {@inheritdoc} */ @@ -104,7 +111,14 @@ public function getIds() */ protected function _doFetch($id) { - return unserialize(file_get_contents($this->getFileName($id))); + //LifetimeFileCache will perform a fetch during contains(), so by + //retaining that data we can greatly speedup the subsequent fetch() + if ($id != $this->_lastId) { + $this->_lastId=$id; + $this->_lastData=unserialize(file_get_contents($this->getFileName($id))); + } + + return $this->_lastData; } /** @@ -120,6 +134,8 @@ protected function _doContains($id) */ protected function _doSave($id, $data, $lifeTime = 0) { + $this->_lastId=$id; + $this->_lastData=$data; return (bool) file_put_contents($this->getFileName($id), serialize($data)); } @@ -127,12 +143,17 @@ protected function _doSave($id, $data, $lifeTime = 0) * {@inheritdoc} */ protected function _doDelete($id) - { + { $file = $this->getFileName($id); if (file_exists($file)) { return unlink($file); } + if ($id == $this->_lastId) { + $this->_lastId=null; + $this->_lastData=null; + } + return true; }