Skip to content

Commit 59c6c50

Browse files
committed
MCP-103: L2 cache local flushing issues
1 parent 9a655bb commit 59c6c50

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

lib/internal/Magento/Framework/Cache/Backend/RemoteSynchronizedCache.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
6161
'remote_backend_options' => [],
6262
'local_backend' => '',
6363
'local_backend_options' => [],
64+
'local_backend_max_size' => 500,
6465
'local_backend_custom_naming' => true,
6566
'local_backend_autoload' => true,
6667
'use_stale_cache' => false,
@@ -237,6 +238,10 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
237238
$dataToSave = $data;
238239
$remHash = $this->loadRemoteDataVersion($id);
239240

241+
if ($this->checkLocalCacheSpace()) {
242+
$this->local->clean();
243+
}
244+
240245
if ($remHash !== false && $this->getDataVersion($data) === $remHash) {
241246
$dataToSave = $this->remote->load($id);
242247
} else {
@@ -251,6 +256,41 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
251256
return $this->local->save($dataToSave, $id, [], $specificLifetime);
252257
}
253258

259+
/**
260+
* Check if local cache space bigger that configure amount
261+
*
262+
* @return bool
263+
*/
264+
private function checkLocalCacheSpace()
265+
{
266+
return
267+
(
268+
$this->getDirectorySize($this->_options['local_backend_options']['cache_dir'])
269+
) / 1024 / 1024 >=
270+
$this->_options['local_backend_max_size'];
271+
}
272+
273+
/**
274+
* Get directory size
275+
*
276+
* @param $directory
277+
* @return int
278+
*/
279+
private function getDirectorySize($directory)
280+
{
281+
$it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
282+
$ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
283+
284+
$size = 0;
285+
foreach ($ri as $file) {
286+
if ($file->isFile()) {
287+
$size += $file->getSize();
288+
}
289+
}
290+
291+
return $size;
292+
}
293+
254294
/**
255295
* @inheritdoc
256296
*/
@@ -266,7 +306,8 @@ public function remove($id)
266306
*/
267307
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
268308
{
269-
return $this->remote->clean($mode, $tags);
309+
return $this->remote->clean($mode, $tags) &&
310+
$this->local->clean($mode, $tags);
270311
}
271312

272313
/**

0 commit comments

Comments
 (0)