Skip to content

Commit

Permalink
Clear entity cache to prevent memory leak (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Coder264 authored Feb 20, 2023
1 parent 200c4d8 commit 4e2bbca
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->fixParametersFromDoctrineEventSubscriberTag($container, [
'simplethings_entityaudit.log_revisions_listener',
'simplethings_entityaudit.create_schema_listener',
'simplethings_entityaudit.cache_listener',
]);
}

Expand Down
38 changes: 38 additions & 0 deletions src/EventListener/CacheListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use SimpleThings\EntityAudit\AuditReader;

final class CacheListener implements EventSubscriber
{
private AuditReader $auditReader;

public function __construct(AuditReader $auditReader)
{
$this->auditReader = $auditReader;
}

public function getSubscribedEvents(): array
{
return [Events::onClear];
}

public function onClear(): void
{
$this->auditReader->clearEntityCache();
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;
use SimpleThings\EntityAudit\AuditReader;
use SimpleThings\EntityAudit\EventListener\CacheListener;
use SimpleThings\EntityAudit\EventListener\CreateSchemaListener;
use SimpleThings\EntityAudit\EventListener\LogRevisionsListener;
use SimpleThings\EntityAudit\User\TokenStorageUsernameCallable;
Expand Down Expand Up @@ -69,6 +70,10 @@
->tag('doctrine.event_subscriber', ['connection' => '%simplethings.entityaudit.connection%'])
->args([new ReferenceConfigurator('simplethings_entityaudit.manager')])

->set('simplethings_entityaudit.cache_listener', CacheListener::class)
->tag('doctrine.event_subscriber', ['connection' => '%simplethings.entityaudit.connection%'])
->args([new ReferenceConfigurator('simplethings_entityaudit.reader')])

->set('simplethings_entityaudit.username_callable.token_storage', TokenStorageUsernameCallable::class)
->args([new ReferenceConfigurator('security.token_storage')])

Expand Down

0 comments on commit 4e2bbca

Please sign in to comment.