Login to bookmark this video
Buy Access to Course
05.

Configuring DoctrineCacheBundle

Share this awesome video!

|

Keep on Learning!

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

Login Subscribe

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:

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

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