Skip to content

Feature Request: Prune caches probabilistically at the end of requests #62277

@jack-worman

Description

@jack-worman

Description

I want a subscriber added to the framework bundle that will sometimes prune caches at the end of a request.

You should be able to configure the PROBABILITY_NUMERATOR and PROBABILITY_DENOMINATOR exactly like the session.gc ini settings.

Example

class PruneCachesSubscriber implements EventSubscriberInterface
{
    private const int PROBABILITY_NUMERATOR = 1;
    private const int PROBABILITY_DENOMINATOR = 10_000;

    public function __construct(
        /** @var iterable<PruneableInterface> */
        private readonly iterable $prunableCaches,
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            TerminateEvent::class => 'pruneCaches',
            ConsoleTerminateEvent::class => 'pruneCaches',
        ];
    }

    public function pruneCaches(): void
    {
        if (\random_int(1, self::PROBABILITY_DENOMINATOR) > self::PROBABILITY_NUMERATOR) {
            return;
        }
        foreach ($this->prunableCaches as $prunableCache) {
            $prunableCache->prune();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions