Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Caching in the prod Environment Only

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

We absolutely need to cache our markdown processing. But what if we need to tweak how the markdown renders? In that case, we don't want caching. Could we somehow disable caching in the dev environment only?

Yes! Copy the doctrine_cache from config.yml and paste it into config_dev.yml. Next, change type from file_system to array:

... lines 1 - 46
doctrine_cache:
providers:
my_markdown_cache:
type: array

The array type is basically a "fake" cache: it won't ever store anything.

Yea, that's it! Head back to the browser and refresh. This is definitely not caching: it takes an entire second because of the sleep(). Try again. Still not caching! Now, change to the prod environment and refresh. Beautiful, this is still really, really fast. Well that was easy.

Clearing prod Cache

Ok, this did work, but there's a small gotcha we're not considering. In config.yml, change that thousands_separator back to a comma:

... lines 1 - 35
# Twig Configuration
twig:
... lines 38 - 39
number_format:
thousands_separator: ','
... lines 42 - 79

Try this first in the dev environment: Yep! No problems. Now refresh in the prod environment. Huh, it's still a period. What gives?

Here's the thing: the prod environment is primed for speed. And that means, when you change any configuration, you need to manually clear the cache before you'll see those changes.

How do you do that? Simple: in the terminal, run:

./bin/console cache:clear --env=prod

You see, even the console script executes your app in a specific environment. By default it uses the dev environment. Normally, you don't really care. But for a few commands, you'll want to switch using this flag.

Ok, back to the browser. Refresh in prod. Boom! There's our comma!

The other Files: services.yml, security.yml, etc

Alright, what about all of these other configuration files. It turns out, there all part of the exact same configuration system we've just mastered. So where is parameters.yml loaded? Well, at the top of config.yml its imported, along with security.yml and services.yml:

imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
... lines 5 - 79

The key point is that all of the files are just loading each other: it's all the same system.

In fact, I could copy all of security.yml, paste it into config.yml, completely delete security.yml and everything would be fine. In fact, the only reason security.yml even exists is because it feels good to keep that stuff in its own file. The same goes for services.yml - a big topic we'll talk about in the future.

Now, parameters.yml is a little special. Let's find out why.

Leave a comment!

11
Login or Register to join the conversation
Default user avatar

Hi, in my config.yml there is no doctrine cache section. It stops at spool: {type: memory}
If I write it in that place it shows me:


There is no extension able to load the configuration for "doctrine_cache" (in C:\aqua_note\app/config\config.yml). Looked for namespace "doctrine_cache", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution".

What I'm missing?
thank you

Reply

Hey Josh,

Have you properly installed this bundle, i.e. download it with Composer and register this bundle in AppKernel? Please, double check it. We did all the installation and configuration of it here: https://knpuniversity.com/s... . Also, try to clear the cache.

Cheers!

Reply
Default user avatar

I've just noticed that by accident I started this course after the first videos. Now, I started from the beginning and it's everything ok, thank you.

Reply
Default user avatar
Default user avatar Richie Hamburg | posted 5 years ago

Setting the doctrine_cache type to array to "stop logging" is pretty ugly. Why would there not be an "enabled" flag?

Reply

Hey Richie Hamburg!

Ah, great question! The point of this configuration is that this will create a "cache" service for us - specifically this creates doctrine_cache.providers.my_markdown_cache. We're then referencing and using this service in our code. So, one way or another, we DO need this service to always exist... which means that in dev environment, we need this service to exist... but not actually to do any real caching. By setting the type to array it effectively means "create a cache object, but of a type that doesn't actually cache". But I totally hear you - the bundle (DoctrineCacheBundle) could have setup their configuration so that, instead of a type: array, you could set some enabled: false flag, and behind the scenes, this would switch you to the "array" type automatically (or to the "void" type - which truly caches nothing - the array type at least "caches" within a process, so that if you ask for a value 2 times, it only calculates it once). It's just a matter of taste - I like the enabled: false idea, but also, since that would change the type behind-the-scenes, the type: array or type: void might be clearer to some people!

Cheers!

Reply
Default user avatar
Default user avatar Chris R | posted 5 years ago

Hi, Team knpuniversity.com. You may hear more from me because I'm working through the Symfony3 courses with the ultimate goal of starting work on a project in Symfony4, so I'll be around here for a while....

When I get about a minute into this video, the step to refresh the prod page (http://localhost:8000/app.php/genus/octopus) results in a 500 error. The dev side works fine. Here's the error from the server log:

[2018-01-29 00:42:18] request.CRITICAL: Uncaught PHP Exception Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: "You have requested a non-existent service "doctrine_cache.providers.my_markdown_cache"." at D:\Users\c\src\ctr-symfony3-fundamentals\var\bootstrap.php.cache line 2129 {"exception":"[object] (Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException(code: 0): You have requested a non-existent service \"doctrine_cache.providers.my_markdown_cache\". at D:\\Users\\c\\src\\ctr-symfony3-fundamentals\\var\\bootstrap.php.cache:2129)"} []

Can you help me figure out what's causing this? I've tried duplicating the code for the caching in config.yml to config_prod.yml as a last resort to make sure the service wasn't missing, but that didn't help so I removed it. I'm using the composer.json from the course download, so Symfony 3.1.4 on PHP 7.2.1, and I'm using the server:run command to execute the code.

Thank you!

Reply

Hey Chris R

We are happy to hear from you :)
I believe you just need to clear cache for your prod environment. Prod environment caches a lot of things and you have to manually specify whenever you want to clear it ($ php bin/console cache:clear -e prod)

If that's not the case, let us know! Have a nice day

1 Reply
Default user avatar

That did it, thanks!

Reply
Default user avatar
Default user avatar Jeroen Noten | posted 5 years ago

Why do I get this warning when clearing the cache?

[WARNING] Calling cache:clear without the --no-warmup option is deprecated since version 3.3. Cache warmup should be
done with the cache:warmup command instead.

Reply

Hey Jeroen,

You get this warning because cache:clear command will only clear the cache in Symfony 4.0. For now, this command clear the cache and then warm it up. So the correct way for Symfony 3.3 is:


./bin/console cache:clear --no-warmup
./bin/console cache:warmup

and you won't get this warning anymore.

But since Symfony 4.0 you can just do:


./bin/console cache:clear
./bin/console cache:warmup

Because cache:clear won't warm up the cache anymore.

Cheers!

Reply
Default user avatar
Default user avatar Jeroen Noten | Victor | posted 5 years ago

allright, thanks!

48 Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4" // 1.4.2
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0" // v3.1.3
    }
}
userVoice