Skip to content

Cache stampede prevention generates a PHP notice #33838

Closed
@dharple

Description

@dharple

Symfony version(s) affected: 4.3.4

Description
When using a TagAwareAdapter for caching, and also using cache contracts, the cache stampede logic generates a PHP notice due to a missing property $logger.

How to reproduce
I'm not sure how to write a unit test for this, because we're relying on a random event, but I've written a console command that brute forces it.

    <?php

    namespace App\Command;

    use Symfony\Component\Cache\Adapter\FilesystemAdapter;
    use Symfony\Component\Cache\Adapter\TagAwareAdapter;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Contracts\Cache\ItemInterface;

    class TestStampedeTagAwareAdapterCommand extends Command
    {
        protected static $defaultName = 'test:stampede-tagawareadapter';

        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $cache = new TagAwareAdapter(
                new FilesystemAdapter(
                    'test',
                    0,
                    '/tmp/test'
                )
            );

            for ($x = 0; $x < 1000000; $x++) {
                $value = $cache->get('test', function (ItemInterface $item) {
                    $item->expiresAfter(15);
                    sleep(1);
                    return 'test.data';
                });
            }
        }
    }

Run this using bin/console test:stampede-tagawareadapter

Note that this may drive load up on your machine, because we're brute forcing the problem.

Possible Solution
Other cache adapters use Psr\Log\LoggerAwareTrait to implement a $logger variable. My suggestion would be to use LoggerAwareTrait in Symfony\Contracts\Cache\CacheTrait.

Additional context
The console output reads as : In CacheTrait.php line 59: Notice: Undefined property: Symfony\Component\Cache\Adapter\TagAwareAdapter::$logger.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions