@@ -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,31 @@ 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- }
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+ );
122129 }
123130 return $ result ;
124131 }
125132
133+ $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
126134 $ cacheData = $ this ->storage ->bulkRead ($ storageKeys );
127- $ result = [];
128135 foreach ($ keys as $ i => $ key ) {
129136 $ storageKey = $ storageKeys [$ i ];
130137 if (isset ($ cacheData [$ storageKey ])) {
131138 $ 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 ]);
135142 });
136143 } else {
137144 $ result [$ key ] = null ;
@@ -279,15 +286,14 @@ public function call(callable $function)
279286 public function wrap (callable $ function , array $ dependencies = null ): \Closure
280287 {
281288 return function () use ($ function , $ dependencies ) {
282- $ key = [$ function , func_get_args ()];
289+ $ key = [$ function , $ args = func_get_args ()];
283290 if (is_array ($ function ) && is_object ($ function [0 ])) {
284291 $ key [0 ][0 ] = get_class ($ function [0 ]);
285292 }
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+ });
291297 };
292298 }
293299
0 commit comments