protected function replace($key, $data)
{
// Write a new database to a temporary file
$tmp = $this->getDatabase()->openTempFile();
$filePointer = $this->getDatabase()->openFile(Database::FILE_READ);
foreach ($filePointer as $line) {
$lineKey = $this->getKeyFromLine($line);
if ($lineKey == $key) {
if ($data !== false) {
$tmp->fwrite($this->getLineString($key, $data));
}
} else {
$tmp->fwrite($line . "\n");
}
}
$this->getDatabase()->closeFile($filePointer);
$tmp->rewind();
// Overwrite the database with the temporary file
$filePointer = $this->getDatabase()->openFile(Database::FILE_WRITE);
foreach ($tmp as $line) {
$filePointer->fwrite($line);
}
$this->getDatabase()->closeFile($filePointer);
$tmp = null;
// Delete the key from cache
if ($cache = $this->getConfig()->getCache()) {
$cache->delete($key);
}
}