Skip to content

Commit 8ffba85

Browse files
committed
Speedup the FileRepository by introducing internal cache
1 parent 1eeb503 commit 8ffba85

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/FileRepository.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class FileRepository implements Repository
66
{
77
private $filename;
88
private $identifier;
9+
private $cache;
910

1011
public function __construct($filename, ObjectIdentifier $identifier)
1112
{
@@ -63,13 +64,18 @@ private function stringify($object)
6364

6465
private function loadDb()
6566
{
66-
return file_exists($this->filename)
67-
? unserialize(file_get_contents($this->filename))
68-
: [];
67+
if (null === $this->cache) {
68+
$this->cache = file_exists($this->filename)
69+
? unserialize(file_get_contents($this->filename))
70+
: [];
71+
}
72+
73+
return $this->cache;
6974
}
7075

7176
private function saveDb(array $collection)
7277
{
7378
file_put_contents($this->filename, serialize($collection));
79+
$this->cache = null;
7480
}
7581
}

0 commit comments

Comments
 (0)