-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathBasicStore.php
More file actions
202 lines (148 loc) · 5.09 KB
/
BasicStore.php
File metadata and controls
202 lines (148 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Statamic\Stache\Stores;
use Statamic\Facades\File;
use Statamic\Facades\Stache;
use Symfony\Component\Finder\SplFileInfo;
abstract class BasicStore extends Store
{
public function getItemFilter(SplFileInfo $file)
{
return $file->getExtension() === 'yaml';
}
abstract public function makeItemFromFile($path, $contents);
public function getItems($keys)
{
$this->handleFileChanges();
$keys = collect($keys);
if ($keys->isEmpty()) {
return collect();
}
// Only use batch fetch for cache drivers that benefit from it (network-based)
if ($this->shouldUseBatchCaching()) {
return $this->getItemsBatched($keys);
}
return $keys->map(fn ($key) => $this->getItem($key));
}
protected function getItemsBatched($keys)
{
// Build a map of cache keys to item keys
$cacheKeyMap = $keys->mapWithKeys(fn ($key) => [$this->getItemCacheKey($key) => $key]);
// Batch fetch from cache
$cached = Stache::cacheStore()->many($cacheKeyMap->keys()->all());
// Process results, fetching any misses from disk
return $keys->map(function ($key) use ($cached) {
$cacheKey = $this->getItemCacheKey($key);
if ($item = $cached[$cacheKey] ?? null) {
if (method_exists($item, 'syncOriginal')) {
$item->syncOriginal();
}
return $item;
}
// Cache miss - fetch individually (will also cache it)
return $this->getItem($key);
});
}
protected function shouldUseBatchCaching(): bool
{
$store = Stache::cacheStore()->getStore();
// These drivers benefit from batch operations (network round-trip reduction)
return $store instanceof \Illuminate\Cache\RedisStore
|| $store instanceof \Illuminate\Cache\MemcachedStore
|| $store instanceof \Illuminate\Cache\DynamoDbStore;
}
public function getItem($key)
{
$this->handleFileChanges();
if (! $path = $this->getPath($key)) {
return null;
}
if ($item = $this->getCachedItem($key)) {
if (method_exists($item, 'syncOriginal')) {
$item->syncOriginal();
}
return $item;
}
$item = $this->makeItemFromFile($path, File::get($path));
$this->cacheItem($item);
if (method_exists($item, 'syncOriginal')) {
$item->syncOriginal();
}
return $item;
}
public function getItemValues($keys, $valueIndex, $keyIndex)
{
// This is for performance. There's no need to resolve anything
// else if we're looking for the keys. We have them already.
if ($valueIndex === 'id' && ! $keyIndex) {
return $keys;
}
$values = $this->getIndexedValues($valueIndex, $keys);
if (! $keyIndex) {
return $values->values();
}
$keyValues = $this->getIndexedValues($keyIndex, $keys);
return $keys->mapWithKeys(fn ($key) => [$keyValues[$key] => $values[$key]]);
}
private function getIndexedValues($name, $only)
{
// We don't want *all* the values in the index. We only want the requested keys. They are
// provided as an array of IDs. It's faster to do has() than contains() so we'll flip them.
$only = $only->flip();
return $this->resolveIndex($name)
->load()
->items()
->filter(fn ($value, $key) => $only->has($key));
}
protected function getCachedItem($key)
{
$cacheKey = $this->getItemCacheKey($key);
return Stache::cacheStore()->get($cacheKey);
}
protected function cacheItem($item)
{
$key = $this->getItemKey($item);
$cacheKey = $this->getItemCacheKey($key);
Stache::cacheStore()->forever($cacheKey, $item);
}
public function forgetItem($key)
{
Stache::cacheStore()->forget($this->getItemCacheKey($key));
}
protected function getItemCacheKey($key)
{
return "stache::items::{$this->key()}::{$key}";
}
protected function getPath($key)
{
return $this->paths()->get($key);
}
protected function getKeyFromPath($path)
{
return $this->paths()->flip()->get($path);
}
public function save($item)
{
$this->writeItemToDisk($item);
$key = $this->getItemKey($item);
$this->forgetItem($key);
$this->setPath($key, $item->path());
$this->resolveIndexes()->each->updateItem($item);
$this->cacheItem($item);
}
public function delete($item)
{
$this->deleteItemFromDisk($item);
$key = $this->getItemKey($item);
$this->forgetItem($key);
$this->forgetPath($key);
$this->resolveIndexes()->filter->isCached()->each->forgetItem($key);
}
protected function writeItemToDisk($item)
{
$item->writeFile();
}
protected function deleteItemFromDisk($item)
{
$item->deleteFile();
}
}