@@ -84,13 +84,19 @@ public function derive(string $namespace)
8484 * @param mixed $key
8585 * @return mixed
8686 */
87- public function load ($ key , callable $ fallback = null )
87+ public function load ($ key , callable $ generator = null )
8888 {
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 );
94100 }
95101 return $ data ;
96102 }
@@ -99,7 +105,7 @@ public function load($key, callable $fallback = null)
99105 /**
100106 * Reads multiple items from the cache.
101107 */
102- public function bulkLoad (array $ keys , callable $ fallback = null ): array
108+ public function bulkLoad (array $ keys , callable $ generator = null ): array
103109 {
104110 if (count ($ keys ) === 0 ) {
105111 return [];
@@ -109,30 +115,29 @@ public function bulkLoad(array $keys, callable $fallback = null): array
109115 throw new Nette \InvalidArgumentException ('Only scalar keys are allowed in bulkLoad() ' );
110116 }
111117 }
112- $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
118+
119+ $ result = [];
113120 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 ]);
121125 }
122- }
126+ : null
127+ );
123128 }
124129 return $ result ;
125130 }
126131
132+ $ storageKeys = array_map ([$ this , 'generateKey ' ], $ keys );
127133 $ cacheData = $ this ->storage ->bulkRead ($ storageKeys );
128- $ result = [];
129134 foreach ($ keys as $ i => $ key ) {
130135 $ storageKey = $ storageKeys [$ i ];
131136 if (isset ($ cacheData [$ storageKey ])) {
132137 $ 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 ]);
136141 });
137142 } else {
138143 $ result [$ key ] = null ;
@@ -280,15 +285,14 @@ public function call(callable $function)
280285 public function wrap (callable $ function , array $ dependencies = null ): \Closure
281286 {
282287 return function () use ($ function , $ dependencies ) {
283- $ key = [$ function , func_get_args ()];
288+ $ key = [$ function , $ args = func_get_args ()];
284289 if (is_array ($ function ) && is_object ($ function [0 ])) {
285290 $ key [0 ][0 ] = get_class ($ function [0 ]);
286291 }
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+ });
292296 };
293297 }
294298
0 commit comments