Skip to content

Instantly share code, notes, and snippets.

@adrenalinkin
Last active June 7, 2021 19:27
Show Gist options
  • Save adrenalinkin/b4be2d9d43a251d218f44998af97a91a to your computer and use it in GitHub Desktop.
Save adrenalinkin/b4be2d9d43a251d218f44998af97a91a to your computer and use it in GitHub Desktop.
Docker for Mac OS. Symfony cache clear optimization

How ist's work?

Leave cache and logs out of synchronize with host machine and we have zero cost overhead when cache has been recalculated

Add ENV variables

PROJECT_NAME=my-project-name

# for linux
SYMFONY_CACHE_DIR=
SYMFONY_LOGS_DIR=
# for Mac OSX
SYMFONY_CACHE_DIR=/tmp/symfony
SYMFONY_LOGS_DIR=/tmp/symfony

Extend docker-compose file

services:
    php:
        container_name: ${PROJECT_NAME}_php
        image: php:7.2-fpm
        volumes:
            - app_volume:/app:rw
        environment:
            SYMFONY_CACHE_DIR: ${SYMFONY_CACHE_DIR}
            SYMFONY_LOGS_DIR: ${SYMFONY_LOGS_DIR}

Symfony 3 and lower

class AppKernel extends Kernel
{
    public function getCacheDir(): string
    {
        $cacheRootDir = (string) getenv('SYMFONY_CACHE_DIR');

        if (empty($cacheRootDir)) {
            return parent::getCacheDir();
        }

        return rtrim($cacheRootDir, '/') . '/cache/' . $this->environment;
    }

    public function getLogDir(): string
    {
        $logsRootDir = (string) getenv('SYMFONY_LOGS_DIR');

        if (empty($logsRootDir)) {
            return parent::getLogDir();
        }

        return rtrim($logsRootDir, '/') . '/logs';
    }
}

Symfony 4 and higher

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    public function getCacheDir(): string
    {
        $cacheRootDir = getenv('SYMFONY_CACHE_DIR') ?? $this->getProjectDir();
    
        return rtrim($cacheRootDir, '/') . '/var/cache/' . $this->environment;
    }

    public function getLogDir(): string
    {
        $logsRootDir = getenv('SYMFONY_LOGS_DIR') ?? $this->getProjectDir();
    
        return rtrim($logsRootDir, '/') . '/var/log';
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment