|
16 | 16 | use Cache\Taggable\TaggableItemInterface;
|
17 | 17 | use Cache\Taggable\TaggablePoolInterface;
|
18 | 18 | use Cache\Taggable\TaggablePoolTrait;
|
| 19 | +use League\Flysystem\FileExistsException; |
19 | 20 | use League\Flysystem\FileNotFoundException;
|
20 | 21 | use League\Flysystem\Filesystem;
|
21 | 22 | use Psr\Cache\CacheItemInterface;
|
@@ -64,19 +65,25 @@ public function setFolder($folder)
|
64 | 65 | */
|
65 | 66 | protected function fetchObjectFromCache($key)
|
66 | 67 | {
|
67 |
| - $file = $this->getFilePath($key); |
| 68 | + $empty = [false, null, []]; |
| 69 | + $file = $this->getFilePath($key); |
68 | 70 | 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; |
70 | 78 | }
|
71 | 79 |
|
72 |
| - $data = unserialize($this->filesystem->read($file)); |
73 | 80 | if ($data[0] !== null && time() > $data[0]) {
|
74 | 81 | foreach ($data[2] as $tag) {
|
75 | 82 | $this->removeListItem($this->getTagKey($tag), $key);
|
76 | 83 | }
|
77 | 84 | $this->forceClear($key);
|
78 | 85 |
|
79 |
| - return [false, null, []]; |
| 86 | + return $empty; |
80 | 87 | }
|
81 | 88 |
|
82 | 89 | return [true, $data[1], $data[2]];
|
@@ -108,21 +115,31 @@ protected function clearOneObjectFromCache($key)
|
108 | 115 | */
|
109 | 116 | protected function storeItemInCache(CacheItemInterface $item, $ttl)
|
110 | 117 | {
|
111 |
| - $file = $this->getFilePath($item->getKey()); |
112 |
| - if ($this->filesystem->has($file)) { |
113 |
| - $this->filesystem->delete($file); |
114 |
| - } |
115 |
| - |
116 | 118 | $tags = [];
|
117 | 119 | if ($item instanceof TaggableItemInterface) {
|
118 | 120 | $tags = $item->getTags();
|
119 | 121 | }
|
120 | 122 |
|
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 | + } |
126 | 143 | }
|
127 | 144 |
|
128 | 145 | /**
|
|
0 commit comments