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