phpcachingdoctrinememcachedapcu

Cannot clear APCu Cache from Console, it's shared in the Webserver memory and not accessible from the CLI


I'm trying to clear doctrine cache after updating entities, so that there won't be errors like this after adding new property:

Uncaught Error: Typed property Project\Entities\Entity::$property must not be accessed before initialization in ...

php cli-config.php orm:clear-cache:metadata and php cli-config.php orm:clear-cache:result works fine but when I'm trying to execute php cli-config.php orm:clear-cache:query I get Error:

In QueryCommand.php line 43:
                                                                                                              
  Cannot clear APCu Cache from Console, it's shared in the Webserver memory and not accessible from the CLI.

If I disable APCu for CLI in php.ini like this:

apc.enable_cli=0

then orm:clear-cache:query results in:

In MemcachedAdapter.php line 300:
                                                     
  MemcachedAdapter client error: connection failure

as Doctrine defaults to MemcachedAdapter if APCu is unavailable.

When creating EntityManager with isDevMode set to true, it works fine (no errors occur) but I'm not convinced it's a valid solution.

new EntityManager(
    DriverManager::getConnection($params),
    ORMSetup::createAttributeMetadataConfiguration(
        paths: [__DIR__ . '/../Entities'],
        isDevMode: true, // should be false as it's production
        proxyDir: __DIR__ . '/../../../tmp',
    ),
);

Versions:

  1. PHP: 8.2
  2. Doctrine ORM: 3.1
  3. Doctrine DBAL: 4.0

I'm not using Symfony, just plain Doctrine.

How to approach this issue so that I could correctly clear the production cache?


Solution

  • Indeed, Doctrine's official documentation states that :

    None of these tasks will work with APC, APCu, or XCache drivers because the memory that the cache is stored in is only accessible to the webserver.

    You can then act directly on the webserver using PHP commands in a webpage (not through CLI).

    Here's the code I personally use:

    $doctrine = orm_instance();
        
    echo "getResultCache() CLEAR <br>";
    $cacheDriver = $doctrine->em->getConfiguration()->getResultCache();
    $cacheDriver->clear();
                    
    echo "getQueryCache() CLEAR <br>";
    $cacheDriver = $doctrine->em->getConfiguration()->getQueryCache();
    $cacheDriver->clear();
                    
    echo "getMetadataCache() CLEAR <br>";
    $cacheDriver = $doctrine->em->getConfiguration()->getMetadataCache();
    $cacheDriver->clear();
    
    dump($doctrine->em);
    

    Another simple way is to execute the APCu native methods in a PHP webpage too.

    $result = apcu_clear_cache();
    dump(apcu_cache_info());