@@ -83,13 +83,19 @@ public function derive(string $namespace)
83
83
* @param mixed $key
84
84
* @return mixed
85
85
*/
86
- public function load ($ key , callable $ fallback = null )
86
+ public function load ($ key , callable $ generator = null )
87
87
{
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 );
93
99
}
94
100
return $ data ;
95
101
}
@@ -98,7 +104,7 @@ public function load($key, callable $fallback = null)
98
104
/**
99
105
* Reads multiple items from the cache.
100
106
*/
101
- public function bulkLoad (array $ keys , callable $ fallback = null ): array
107
+ public function bulkLoad (array $ keys , callable $ generator = null ): array
102
108
{
103
109
if (count ($ keys ) === 0 ) {
104
110
return [];
@@ -108,30 +114,29 @@ public function bulkLoad(array $keys, callable $fallback = null): array
108
114
throw new Nette \InvalidArgumentException ('Only scalar keys are allowed in bulkLoad() ' );
109
115
}
110
116
}
111
- $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
117
+
118
+ $ result = [];
112
119
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 ]);
120
124
}
121
- }
125
+ : null
126
+ );
122
127
}
123
128
return $ result ;
124
129
}
125
130
131
+ $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
126
132
$ cacheData = $ this ->storage ->bulkRead ($ storageKeys );
127
- $ result = [];
128
133
foreach ($ keys as $ i => $ key ) {
129
134
$ storageKey = $ storageKeys [$ i ];
130
135
if (isset ($ cacheData [$ storageKey ])) {
131
136
$ 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 ]);
135
140
});
136
141
} else {
137
142
$ result [$ key ] = null ;
@@ -279,15 +284,14 @@ public function call(callable $function)
279
284
public function wrap (callable $ function , array $ dependencies = null ): \Closure
280
285
{
281
286
return function () use ($ function , $ dependencies ) {
282
- $ key = [$ function , func_get_args ()];
287
+ $ key = [$ function , $ args = func_get_args ()];
283
288
if (is_array ($ function ) && is_object ($ function [0 ])) {
284
289
$ key [0 ][0 ] = get_class ($ function [0 ]);
285
290
}
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
+ });
291
295
};
292
296
}
293
297
0 commit comments