@@ -61,6 +61,7 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
61
61
'remote_backend_options ' => [],
62
62
'local_backend ' => '' ,
63
63
'local_backend_options ' => [],
64
+ 'local_backend_max_size ' => 500 ,
64
65
'local_backend_custom_naming ' => true ,
65
66
'local_backend_autoload ' => true ,
66
67
'use_stale_cache ' => false ,
@@ -237,6 +238,10 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
237
238
$ dataToSave = $ data ;
238
239
$ remHash = $ this ->loadRemoteDataVersion ($ id );
239
240
241
+ if ($ this ->checkLocalCacheSpace ()) {
242
+ $ this ->local ->clean ();
243
+ }
244
+
240
245
if ($ remHash !== false && $ this ->getDataVersion ($ data ) === $ remHash ) {
241
246
$ dataToSave = $ this ->remote ->load ($ id );
242
247
} else {
@@ -251,6 +256,41 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
251
256
return $ this ->local ->save ($ dataToSave , $ id , [], $ specificLifetime );
252
257
}
253
258
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
+
254
294
/**
255
295
* @inheritdoc
256
296
*/
@@ -266,7 +306,8 @@ public function remove($id)
266
306
*/
267
307
public function clean ($ mode = \Zend_Cache::CLEANING_MODE_ALL , $ tags = [])
268
308
{
269
- return $ this ->remote ->clean ($ mode , $ tags );
309
+ return $ this ->remote ->clean ($ mode , $ tags ) &&
310
+ $ this ->local ->clean ($ mode , $ tags );
270
311
}
271
312
272
313
/**
0 commit comments