Skip to content

Commit 1196bf4

Browse files
committed
Cache: refactoring, load() is used for generating instead of save()
1 parent 3bdb51d commit 1196bf4

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/Caching/Cache.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,19 @@ public function derive(string $namespace)
8383
* @param mixed $key
8484
* @return mixed
8585
*/
86-
public function load($key, callable $fallback = null)
86+
public function load($key, callable $generator = null)
8787
{
88-
$data = $this->storage->read($this->generateKey($key));
89-
if ($data === null && $fallback) {
90-
return $this->save($key, function (&$dependencies) use ($fallback) {
91-
return $fallback(...[&$dependencies]);
92-
});
88+
$storageKey = $this->generateKey($key);
89+
$data = $this->storage->read($storageKey);
90+
if ($data === null && $generator) {
91+
$this->storage->lock($storageKey);
92+
try {
93+
$data = $generator(...[&$dependencies]);
94+
} catch (\Throwable $e) {
95+
$this->storage->remove($storageKey);
96+
throw $e;
97+
}
98+
$this->save($key, $data, $dependencies);
9399
}
94100
return $data;
95101
}
@@ -98,7 +104,7 @@ public function load($key, callable $fallback = null)
98104
/**
99105
* Reads multiple items from the cache.
100106
*/
101-
public function bulkLoad(array $keys, callable $fallback = null): array
107+
public function bulkLoad(array $keys, callable $generator = null): array
102108
{
103109
if (count($keys) === 0) {
104110
return [];
@@ -108,30 +114,29 @@ public function bulkLoad(array $keys, callable $fallback = null): array
108114
throw new Nette\InvalidArgumentException('Only scalar keys are allowed in bulkLoad()');
109115
}
110116
}
111-
$storageKeys = array_map([$this, 'generateKey'], $keys);
117+
118+
$result = [];
112119
if (!$this->storage instanceof BulkReader) {
113-
$result = array_combine($keys, array_map([$this->storage, 'read'], $storageKeys));
114-
if ($fallback !== null) {
115-
foreach ($result as $key => $value) {
116-
if ($value === null) {
117-
$result[$key] = $this->save($key, function (&$dependencies) use ($key, $fallback) {
118-
return $fallback(...[$key, &$dependencies]);
119-
});
120+
foreach ($keys as $key) {
121+
$result[$key] = $this->load($key, $generator
122+
? function (&$dependencies) use ($key, $generator) {
123+
return $generator(...[$key, &$dependencies]);
120124
}
121-
}
125+
: null
126+
);
122127
}
123128
return $result;
124129
}
125130

131+
$storageKeys = array_map([$this, 'generateKey'], $keys);
126132
$cacheData = $this->storage->bulkRead($storageKeys);
127-
$result = [];
128133
foreach ($keys as $i => $key) {
129134
$storageKey = $storageKeys[$i];
130135
if (isset($cacheData[$storageKey])) {
131136
$result[$key] = $cacheData[$storageKey];
132-
} elseif ($fallback) {
133-
$result[$key] = $this->save($key, function (&$dependencies) use ($key, $fallback) {
134-
return $fallback(...[$key, &$dependencies]);
137+
} elseif ($generator) {
138+
$result[$key] = $this->load($key, function (&$dependencies) use ($key, $generator) {
139+
return $generator(...[$key, &$dependencies]);
135140
});
136141
} else {
137142
$result[$key] = null;
@@ -279,15 +284,14 @@ public function call(callable $function)
279284
public function wrap(callable $function, array $dependencies = null): \Closure
280285
{
281286
return function () use ($function, $dependencies) {
282-
$key = [$function, func_get_args()];
287+
$key = [$function, $args = func_get_args()];
283288
if (is_array($function) && is_object($function[0])) {
284289
$key[0][0] = get_class($function[0]);
285290
}
286-
$data = $this->load($key);
287-
if ($data === null) {
288-
$data = $this->save($key, $function(...$key[1]), $dependencies);
289-
}
290-
return $data;
291+
return $this->load($key, function (&$deps) use ($function, $args, $dependencies) {
292+
$deps = $dependencies;
293+
return $function(...$args);
294+
});
291295
};
292296
}
293297

0 commit comments

Comments
 (0)