This tutorial has a new version, check it out!

Configuring DoctrineCacheBundle

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

We have a new toy, I mean service! In GenusController, let's use it: $cache = $this->get('') and start typing markdown. Ah, there's our service!

... lines 1 - 10
class GenusController extends Controller
{
... lines 13 - 15
public function showAction($genusName)
{
$funFact = 'Octopuses can change the color of their body in just *three-tenths* of a second!';
$cache = $this->get('doctrine_cache.providers.my_markdown_cache');
... lines 21 - 34
}
... lines 36 - 53
}

Here's the goal: make sure the same string doesn't get parsed twice through markdown. To do that, create a $key = md5($funFact);:

... lines 1 - 19
$cache = $this->get('doctrine_cache.providers.my_markdown_cache');
$key = md5($funFact);
... lines 22 - 55

To use the cache service, add if ($cache->contains($key)). In this case, just set $funFact = $cache->fetch($key);:

... lines 1 - 19
$cache = $this->get('doctrine_cache.providers.my_markdown_cache');
$key = md5($funFact);
if ($cache->contains($key)) {
$funFact = $cache->fetch($key);
... lines 24 - 28
}
... lines 30 - 55

Else, we're going to need to parse through Markdown.

Let's live dangerously: add a sleep(1); to pretend like our markdown transformation is really taking a long time. Next, parse the fun fact and finish with $cache->save($key, $funFact):

... lines 1 - 21
if ($cache->contains($key)) {
$funFact = $cache->fetch($key);
} else {
sleep(1); // fake how slow this could be
$funFact = $this->get('markdown.parser')
->transform($funFact);
$cache->save($key, $funFact);
}
... lines 30 - 55

Let's do this! Go back to the browser... and watch closely my friends. Refresh! Things are moving a bit slow. Yep: the web debug toolbar shows us 1048 ms. Refresh again. super fast! Just 41 ms: the cache is working! But... uhh... where is this being cached exactly?

Out of the box, the answer is in var/cache. In the terminal, ls var/cache, then ls var/cache/dev:

ls var/cache/dev/

Hey! There's a doctrine directory with cache/file_system inside. There is our cached markdown.

This bundle assumed that this is where we want to cache things. That was really convenient, but clearly, there needs to be a way to control that as well.

Configuring the Cache Path

Rerun config:dump-reference doctrine_cache:

./bin/console config:dump-reference doctrine_cache

Ah, there's a directory key we can use to control things. In the editor, add this new directory key and set it to /tmp/doctrine_cache for now:

... lines 1 - 72
doctrine_cache:
providers:
my_markdown_cache:
type: file_system
directory: /tmp/doctrine_cache

Ok, back to the browser team. Refresh! Poseidon's beard, check out that HUGE error! "Unrecognized option 'directory'". Remember, configuration is validated... which means we just messed something up. Oh yea: I see the problem: I need to have file_system and then directory below that. Add file_system above directory and then indent it to make things match:

... lines 1 - 72
doctrine_cache:
providers:
my_markdown_cache:
type: file_system
file_system:
directory: /tmp/doctrine_cache

Ok, try this out one more time.

It should be slow the first time... yes! And then super fast the second time. In the terminal, we can even see a /tmp/doctrine_cache/ directory:

ls /tmp/doctrine_cache/

Big picture time: bundles give you services, and those services can be controlled in config.yml. Every bundle works a little bit different - but if you understand this basic concept, you're on your way.

Leave a comment!

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
        "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
    }
}