@@ -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,31 @@ 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
- }
121
- }
120
+ foreach ($ keys as $ key ) {
121
+ $ result [ $ key ] = $ this -> load (
122
+ $ key,
123
+ $ generator
124
+ ? function (&$ dependencies ) use ($ key , $ generator ) {
125
+ return $ generator (...[$ key , &$ dependencies ]);
126
+ }
127
+ : null
128
+ );
122
129
}
123
130
return $ result ;
124
131
}
125
132
133
+ $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
126
134
$ cacheData = $ this ->storage ->bulkRead ($ storageKeys );
127
- $ result = [];
128
135
foreach ($ keys as $ i => $ key ) {
129
136
$ storageKey = $ storageKeys [$ i ];
130
137
if (isset ($ cacheData [$ storageKey ])) {
131
138
$ result [$ key ] = $ cacheData [$ storageKey ];
132
- } elseif ($ fallback ) {
133
- $ result [$ key ] = $ this ->save ($ key , function (&$ dependencies ) use ($ key , $ fallback ) {
134
- return $ fallback (...[$ key , &$ dependencies ]);
139
+ } elseif ($ generator ) {
140
+ $ result [$ key ] = $ this ->load ($ key , function (&$ dependencies ) use ($ key , $ generator ) {
141
+ return $ generator (...[$ key , &$ dependencies ]);
135
142
});
136
143
} else {
137
144
$ result [$ key ] = null ;
@@ -279,15 +286,14 @@ public function call(callable $function)
279
286
public function wrap (callable $ function , array $ dependencies = null ): \Closure
280
287
{
281
288
return function () use ($ function , $ dependencies ) {
282
- $ key = [$ function , func_get_args ()];
289
+ $ key = [$ function , $ args = func_get_args ()];
283
290
if (is_array ($ function ) && is_object ($ function [0 ])) {
284
291
$ key [0 ][0 ] = get_class ($ function [0 ]);
285
292
}
286
- $ data = $ this ->load ($ key );
287
- if ($ data === null ) {
288
- $ data = $ this ->save ($ key , $ function (...$ key [1 ]), $ dependencies );
289
- }
290
- return $ data ;
293
+ return $ this ->load ($ key , function (&$ deps ) use ($ function , $ args , $ dependencies ) {
294
+ $ deps = $ dependencies ;
295
+ return $ function (...$ args );
296
+ });
291
297
};
292
298
}
293
299
0 commit comments