Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized performance of LifetimeFileCache by having FileCache retain last fetch #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -120,19 +134,26 @@ 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));
}

/**
* {@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;
}
Expand Down