Skip to content

Commit eeb6a2b

Browse files
authored
Reduce filesystem operations (#94)
* Reduce filesystem operations * Updated changelog * Handle race condition * typo * cs * Handle exception * cs
1 parent 2040282 commit eeb6a2b

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## UNRELEASED
66

7+
### Changed
8+
9+
Using `Filesystem::update` instead of `Filesystem::delete` and `Filesystem::write`.
10+
711
## 0.3.1
812

913
### Added

FilesystemCachePool.php

+31-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cache\Taggable\TaggableItemInterface;
1717
use Cache\Taggable\TaggablePoolInterface;
1818
use Cache\Taggable\TaggablePoolTrait;
19+
use League\Flysystem\FileExistsException;
1920
use League\Flysystem\FileNotFoundException;
2021
use League\Flysystem\Filesystem;
2122
use Psr\Cache\CacheItemInterface;
@@ -64,19 +65,25 @@ public function setFolder($folder)
6465
*/
6566
protected function fetchObjectFromCache($key)
6667
{
67-
$file = $this->getFilePath($key);
68+
$empty = [false, null, []];
69+
$file = $this->getFilePath($key);
6870
if (!$this->filesystem->has($file)) {
69-
return [false, null, []];
71+
return $empty;
72+
}
73+
74+
try {
75+
$data = unserialize($this->filesystem->read($file));
76+
} catch (FileNotFoundException $e) {
77+
return $empty;
7078
}
7179

72-
$data = unserialize($this->filesystem->read($file));
7380
if ($data[0] !== null && time() > $data[0]) {
7481
foreach ($data[2] as $tag) {
7582
$this->removeListItem($this->getTagKey($tag), $key);
7683
}
7784
$this->forceClear($key);
7885

79-
return [false, null, []];
86+
return $empty;
8087
}
8188

8289
return [true, $data[1], $data[2]];
@@ -108,21 +115,31 @@ protected function clearOneObjectFromCache($key)
108115
*/
109116
protected function storeItemInCache(CacheItemInterface $item, $ttl)
110117
{
111-
$file = $this->getFilePath($item->getKey());
112-
if ($this->filesystem->has($file)) {
113-
$this->filesystem->delete($file);
114-
}
115-
116118
$tags = [];
117119
if ($item instanceof TaggableItemInterface) {
118120
$tags = $item->getTags();
119121
}
120122

121-
return $this->filesystem->write($file, serialize([
122-
($ttl === null ? null : time() + $ttl),
123-
$item->get(),
124-
$tags,
125-
]));
123+
$data = serialize(
124+
[
125+
($ttl === null ? null : time() + $ttl),
126+
$item->get(),
127+
$tags,
128+
]
129+
);
130+
131+
$file = $this->getFilePath($item->getKey());
132+
if ($this->filesystem->has($file)) {
133+
// Update file if it exists
134+
return $this->filesystem->update($file, $data);
135+
}
136+
137+
try {
138+
return $this->filesystem->write($file, $data);
139+
} catch (FileExistsException $e) {
140+
// To handle issues when/if race conditions occurs, we try to update here.
141+
return $this->filesystem->update($file, $data);
142+
}
126143
}
127144

128145
/**

0 commit comments

Comments
 (0)