Skip to content

Commit

Permalink
Detach cell parent when flushing cache to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Feb 19, 2024
1 parent 763fe03 commit e1f0930
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Cache/BatchCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Maatwebsite\Excel\Cache;

use Illuminate\Support\Facades\Cache;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use Psr\SimpleCache\CacheInterface;

class BatchCache implements CacheInterface
Expand Down
9 changes: 8 additions & 1 deletion src/Cache/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\Excel\Cache;

use PhpOffice\PhpSpreadsheet\Cell\Cell;
use Psr\SimpleCache\CacheInterface;

class MemoryCache implements CacheInterface
Expand All @@ -17,7 +18,7 @@ class MemoryCache implements CacheInterface
protected $cache = [];

/**
* @param int|null $memoryLimit
* @param int|null $memoryLimit
*/
public function __construct(int $memoryLimit = null)
{
Expand Down Expand Up @@ -131,6 +132,12 @@ public function flush(): array
{
$memory = $this->cache;

foreach ($memory as $cell) {
if ($cell instanceof Cell) {
$cell->detach();
}
}

$this->clear();

return $memory;
Expand Down
1 change: 1 addition & 0 deletions tests/QueuedImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function test_can_queue_an_import_with_batch_cache_and_file_store()
config()->set('queue.default', 'sync');
config()->set('excel.cache.driver', 'batch');
config()->set('excel.cache.illuminate.store', 'file');
config()->set('excel.cache.batch.memory_limit', 80);

// Reset the cache settings
$this->app->make(SettingsProvider::class)->provide();
Expand Down
1 change: 1 addition & 0 deletions tests/QueuedQueryExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function test_can_queue_an_export_with_batch_cache_and_file_store()
config()->set('queue.default', 'sync');
config()->set('excel.cache.driver', 'batch');
config()->set('excel.cache.illuminate.store', 'file');
config()->set('excel.cache.batch.memory_limit', 80);

// Reset the cache settings
$this->app->make(SettingsProvider::class)->provide();
Expand Down

0 comments on commit e1f0930

Please sign in to comment.