Buy

Caching in the prod Environment Only

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:

51 lines app/config/config_dev.yml
... 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:

79 lines app/config/config.yml
... 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:

79 lines app/config/config.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!

  • 2017-07-11 Josk

    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.

  • 2017-07-10 Victor Bocharsky

    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!

  • 2017-07-07 Josk

    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

  • 2017-03-09 weaverryan

    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!

  • 2017-03-08 Richie Hamburg

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