Skip to content

[Cache] FilesystemAdapter cache uses different pool ids between console commands and executed via PHP-FPM #30226

@keichinger

Description

@keichinger

Symfony version(s) affected: At least 4.2.3

Description
Using the FilesystemAdapter from a Symfony CLI command will write the cache to a different pool than doing so via PHP-FPM/your browser

How to reproduce
Reproducer code: https://github.com/keichinger/symfony-filesystem-cache-issue

  1. After installing the project as usual (git clone, composer install, local webserver configs if necessary, etc.)
  2. Run php bin/console cache:clear --env="dev"
  3. Open the file vendor/symfony/cache/Traits/FilesystemCommonTrait.php and add some debug code, which will come in handy while investigating this issue:
    private function write($file, $data, $expiresAt = null)
    {
        // ===============
        // ADDED DEBUG CODE
        dump("writing cache file: " . $file);
        // END OF ADDED DEBUG CODE
        // ===============

        set_error_handler(__CLASS__.'::throwError');
        try {
            if (null === $this->tmp) {
                $this->tmp = $this->directory.uniqid('', true);
            }
            file_put_contents($this->tmp, $data);

            if (null !== $expiresAt) {
                touch($this->tmp, $expiresAt);
            }

            return rename($this->tmp, $file);
        } finally {
            restore_error_handler();
        }
    }

    private function getFile($id, $mkdir = false)
    {
        // Use MD5 to favor speed over security, which is not an issue here
        $hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
        $dir = $this->directory.strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);

        if ($mkdir && !file_exists($dir)) {
            @mkdir($dir, 0777, true);
        }

        // ===============
        // ADDED DEBUG CODE
        if ($id === "something") { dump("reading cache file: " . $dir . substr($hash, 2, 20)); }
        // END OF ADDED DEBUG CODE
        // ===============

        return $dir.substr($hash, 2, 20);
    }
  1. Run the following command: php bin/console app:complex-data:build-cache --env="dev" and make sure you get an output that tells you that clearing and building the cache was successful, along side the paths to the cache file (they're important!)
  2. Open the site in your browser, under / you should get a bunch of debug output, alongside the paths to the cache file (they differ from the CLI!) with the cached object being null since the cache wasn't hit

Additional context
The cache file names do match, however, the pool ids are different between the console command and the app being executed from the browser:

image
image

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