3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Config \App \Config \Type ;
7
8
8
9
use Magento \Framework \App \Config \ConfigSourceInterface ;
13
14
use Magento \Config \App \Config \Type \System \Reader ;
14
15
use Magento \Framework \App \ScopeInterface ;
15
16
use Magento \Framework \Cache \FrontendInterface ;
17
+ use Magento \Framework \Cache \LockQueryInterface ;
16
18
use Magento \Framework \Lock \LockManagerInterface ;
17
19
use Magento \Framework \Serialize \SerializerInterface ;
18
20
use Magento \Store \Model \Config \Processor \Fallback ;
19
- use Magento \Store \Model \ScopeInterface as StoreScope ;
20
21
use Magento \Framework \Encryption \Encryptor ;
22
+ use Magento \Store \Model \ScopeInterface as StoreScope ;
21
23
22
24
/**
23
25
* System configuration type
@@ -42,22 +44,6 @@ class System implements ConfigTypeInterface
42
44
* @var string
43
45
*/
44
46
private static $ lockName = 'SYSTEM_CONFIG ' ;
45
- /**
46
- * Timeout between retrieves to load the configuration from the cache.
47
- *
48
- * Value of the variable in microseconds.
49
- *
50
- * @var int
51
- */
52
- private static $ delayTimeout = 100000 ;
53
- /**
54
- * Lifetime of the lock for write in cache.
55
- *
56
- * Value of the variable in seconds.
57
- *
58
- * @var int
59
- */
60
- private static $ lockTimeout = 42 ;
61
47
62
48
/**
63
49
* @var array
@@ -106,9 +92,9 @@ class System implements ConfigTypeInterface
106
92
private $ encryptor ;
107
93
108
94
/**
109
- * @var LockManagerInterface
95
+ * @var LockQueryInterface
110
96
*/
111
- private $ locker ;
97
+ private $ lockQuery ;
112
98
113
99
/**
114
100
* @param ConfigSourceInterface $source
@@ -122,6 +108,7 @@ class System implements ConfigTypeInterface
122
108
* @param Reader|null $reader
123
109
* @param Encryptor|null $encryptor
124
110
* @param LockManagerInterface|null $locker
111
+ * @param LockQueryInterface|null $lockQuery
125
112
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
126
113
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
127
114
*/
@@ -136,7 +123,8 @@ public function __construct(
136
123
$ configType = self ::CONFIG_TYPE ,
137
124
Reader $ reader = null ,
138
125
Encryptor $ encryptor = null ,
139
- LockManagerInterface $ locker = null
126
+ LockManagerInterface $ locker = null ,
127
+ LockQueryInterface $ lockQuery = null
140
128
) {
141
129
$ this ->postProcessor = $ postProcessor ;
142
130
$ this ->cache = $ cache ;
@@ -145,8 +133,8 @@ public function __construct(
145
133
$ this ->reader = $ reader ?: ObjectManager::getInstance ()->get (Reader::class);
146
134
$ this ->encryptor = $ encryptor
147
135
?: ObjectManager::getInstance ()->get (Encryptor::class);
148
- $ this ->locker = $ locker
149
- ?: ObjectManager::getInstance ()->get (LockManagerInterface ::class);
136
+ $ this ->lockQuery = $ lockQuery
137
+ ?: ObjectManager::getInstance ()->get (LockQueryInterface ::class);
150
138
}
151
139
152
140
/**
@@ -225,91 +213,76 @@ private function getWithParts($path)
225
213
}
226
214
227
215
/**
228
- * Make lock on data load.
229
- *
230
- * @param callable $dataLoader
231
- * @param bool $flush
232
- * @return array
233
- */
234
- private function lockedLoadData (callable $ dataLoader , bool $ flush = false ): array
235
- {
236
- $ cachedData = $ dataLoader (); //optimistic read
237
-
238
- while ($ cachedData === false && $ this ->locker ->isLocked (self ::$ lockName )) {
239
- usleep (self ::$ delayTimeout );
240
- $ cachedData = $ dataLoader ();
241
- }
242
-
243
- while ($ cachedData === false ) {
244
- try {
245
- if ($ this ->locker ->lock (self ::$ lockName , self ::$ lockTimeout )) {
246
- if (!$ flush ) {
247
- $ data = $ this ->readData ();
248
- $ this ->cacheData ($ data );
249
- $ cachedData = $ data ;
250
- } else {
251
- $ this ->cache ->clean (\Zend_Cache::CLEANING_MODE_MATCHING_TAG , [self ::CACHE_TAG ]);
252
- $ cachedData = [];
253
- }
254
- }
255
- } finally {
256
- $ this ->locker ->unlock (self ::$ lockName );
257
- }
258
-
259
- if ($ cachedData === false ) {
260
- usleep (self ::$ delayTimeout );
261
- $ cachedData = $ dataLoader ();
262
- }
263
- }
264
-
265
- return $ cachedData ;
266
- }
267
-
268
- /**
269
- * Load configuration data for all scopes
216
+ * Load configuration data for all scopes.
270
217
*
271
218
* @return array
272
219
*/
273
220
private function loadAllData ()
274
221
{
275
- return $ this -> lockedLoadData ( function () {
222
+ $ loadAction = function () {
276
223
$ cachedData = $ this ->cache ->load ($ this ->configType );
277
224
$ data = false ;
278
225
if ($ cachedData !== false ) {
279
226
$ data = $ this ->serializer ->unserialize ($ this ->encryptor ->decrypt ($ cachedData ));
280
227
}
281
228
return $ data ;
282
- });
229
+ };
230
+ $ loadAction ->bindTo ($ this );
231
+
232
+ $ collectAction = \Closure::fromCallable ([$ this , 'readData ' ]);
233
+ $ saveAction = \Closure::fromCallable ([$ this , 'cacheData ' ]);
234
+ $ cleanAction = \Closure::fromCallable ([$ this , 'cleanCacheAction ' ]);
235
+
236
+ return $ this ->lockQuery ->lockedLoadData (
237
+ self ::$ lockName ,
238
+ $ loadAction ,
239
+ $ collectAction ,
240
+ $ saveAction ,
241
+ $ cleanAction
242
+ );
283
243
}
284
244
285
245
/**
286
- * Load configuration data for default scope
246
+ * Load configuration data for default scope.
287
247
*
288
248
* @param string $scopeType
289
249
* @return array
290
250
*/
291
251
private function loadDefaultScopeData ($ scopeType )
292
252
{
293
- return $ this -> lockedLoadData ( function () use ($ scopeType ) {
253
+ $ loadAction = function () use ($ scopeType ) {
294
254
$ cachedData = $ this ->cache ->load ($ this ->configType . '_ ' . $ scopeType );
295
255
$ scopeData = false ;
296
256
if ($ cachedData !== false ) {
297
257
$ scopeData = [$ scopeType => $ this ->serializer ->unserialize ($ this ->encryptor ->decrypt ($ cachedData ))];
298
258
}
299
259
return $ scopeData ;
300
- });
260
+ };
261
+ $ loadAction ->bindTo ($ this );
262
+
263
+ $ collectAction = \Closure::fromCallable ([$ this , 'readData ' ]);
264
+ $ saveAction = \Closure::fromCallable ([$ this , 'cacheData ' ]);
265
+ $ cleanAction = \Closure::fromCallable ([$ this , 'cleanCacheAction ' ]);
266
+
267
+ return $ this ->lockQuery ->lockedLoadData (
268
+ self ::$ lockName ,
269
+ $ loadAction ,
270
+ $ collectAction ,
271
+ $ saveAction ,
272
+ $ cleanAction
273
+ );
301
274
}
302
275
303
276
/**
304
- * Load configuration data for a specified scope
277
+ * Load configuration data for a specified scope.
305
278
*
306
279
* @param string $scopeType
307
280
* @param string $scopeId
308
281
* @return array
309
282
*/
310
283
private function loadScopeData ($ scopeType , $ scopeId )
311
284
{
312
- return $ this -> lockedLoadData ( function () use ($ scopeType , $ scopeId ) {
285
+ $ loadAction = function () use ($ scopeType , $ scopeId ) {
313
286
$ cachedData = $ this ->cache ->load ($ this ->configType . '_ ' . $ scopeType . '_ ' . $ scopeId );
314
287
$ scopeData = false ;
315
288
if ($ cachedData === false ) {
@@ -329,7 +302,20 @@ private function loadScopeData($scopeType, $scopeId)
329
302
}
330
303
331
304
return $ scopeData ;
332
- });
305
+ };
306
+ $ loadAction ->bindTo ($ this );
307
+
308
+ $ collectAction = \Closure::fromCallable ([$ this , 'readData ' ]);
309
+ $ saveAction = \Closure::fromCallable ([$ this , 'cacheData ' ]);
310
+ $ cleanAction = \Closure::fromCallable ([$ this , 'cleanCacheAction ' ]);
311
+
312
+ return $ this ->lockQuery ->lockedLoadData (
313
+ self ::$ lockName ,
314
+ $ loadAction ,
315
+ $ collectAction ,
316
+ $ saveAction ,
317
+ $ cleanAction
318
+ );
333
319
}
334
320
335
321
/**
@@ -371,7 +357,17 @@ private function cacheData(array $data)
371
357
}
372
358
373
359
/**
374
- * Walk nested hash map by keys from $pathParts
360
+ * Clean cache action.
361
+ *
362
+ * @return void
363
+ */
364
+ private function cleanCacheAction ()
365
+ {
366
+ $ this ->cache ->clean (\Zend_Cache::CLEANING_MODE_MATCHING_TAG , [self ::CACHE_TAG ]);
367
+ }
368
+
369
+ /**
370
+ * Walk nested hash map by keys from $pathParts.
375
371
*
376
372
* @param array $data to walk in
377
373
* @param array $pathParts keys path
@@ -408,7 +404,7 @@ private function readData(): array
408
404
}
409
405
410
406
/**
411
- * Clean cache and global variables cache
407
+ * Clean cache and global variables cache.
412
408
*
413
409
* Next items cleared:
414
410
* - Internal property intended to store already loaded configuration data
@@ -420,10 +416,20 @@ private function readData(): array
420
416
public function clean ()
421
417
{
422
418
$ this ->data = [];
423
- $ this ->lockedLoadData (
424
- function () {
425
- return false ;
426
- },
419
+ $ loadAction = function () {
420
+ return false ;
421
+ };
422
+
423
+ $ collectAction = \Closure::fromCallable ([$ this , 'readData ' ]);
424
+ $ saveAction = \Closure::fromCallable ([$ this , 'cacheData ' ]);
425
+ $ cleanAction = \Closure::fromCallable ([$ this , 'cleanCacheAction ' ]);
426
+
427
+ $ this ->lockQuery ->lockedLoadData (
428
+ self ::$ lockName ,
429
+ $ loadAction ,
430
+ $ collectAction ,
431
+ $ saveAction ,
432
+ $ cleanAction ,
427
433
true
428
434
);
429
435
}
0 commit comments