friky.com
← Back to archive

Post

Beware of accumulating expired Octane cached items

2022-12-06 Agustí Pons Original post

When using swoole tables as Octane cache in Laravel, be aware that the expired cached items are never really deleted. 

The get method of the cache ignores the expired items but they are never flushed if you don't explicitly delete them. 

To avoid the Swoole Cache table to grow ad aeternum you must do some cleanup from time to time.

Define a new class that will do the clean up every minute using the Tick functionality of the Swoole extension. 

$record) {     if ($record\['expiration'\] self::getAndReport())     \->seconds(60);   } } And in the file **app/Providers/RouteServiceProvider.php** call the boot method of the class in order to set the Ticker.  ...  **CacheHouseKeeping::boot();**  ...  Then, every minute the clean function will be executed and will clean all the expired items in the cache. The main lines works as follows:  1\. Get the cache Swoole table from the application service container (the App Facade). **$table=App::make('octane.cacheTable');**  2\. Loop through all the items in the swoole cache table records **foreach ($table as $key => $record) {**  3\. Check the expiration date  **if ($record\['expiration'\]del($key);****  Hope this help you to keep the Octane Swoole Cache under control.