04.
Adding a Cache Service
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
16 Comments
I put this in my services.yml:
and all It's ok but now How configure my config.yml in doctrine-cache?
Thanks
Hey Gianluca,
I think your "doctrine_cache" configuration should be like:
And then you should be able to use the short "memcached_cache" alias to get the memcached provider, instead of using long "doctrine_cache.providers.service_connection_memcached_provider":
$memcachedCache = $this->container->get('memcached_cache');
I hope this helps!
Cheers!
I see there is a lack of doc for using caching, can you provide some simple code example:
for the type:
1- array caching
2- memcache
3- memcached
4 - redis
with some explanation, it will help me a lot for understanding it deeper
Hey Igor,
Those are just different caching systems, own has its advantages on it depends on your project to choose one or another. You can find *a lot* of information on the internet about each one. For the difference between memcache and memcached you can check this answer: https://stackoverflow.com/a... . But "array" type is something interesting, it do nothing, just simulate some kind of caching. It's useful only on your local machine when developing or testing the project, you don't need to write some conditions to cache only on production, that means you can still call caching methods but nothing will be cached, i.e. you don't need to write custom conditions to cache things only in production mode.
Cheers!
Thanks for the quick reply
I tried to find good example for cache memcache and redis and didn't found in symfony project. maybe you can point me out
Can you assist me with this?
Hey Igor,
I don't have an example of memcache, but we do talk about Redis in our Ansible tutorials:
https://knpuniversity.com/s...
https://knpuniversity.com/s...
You can find some examples in course code. But with Memcache actually the same, you just need to specify another cache provider, i.e. Memcache provider instead of Redis one, but methods of Cache Component are the same of it, so you even don't need to tweak any line of code. And of course, you need to install Memcache in your system.
Cheers!
Symfony makes automatic caching with the hole pages, what if i want to disable it for an specific route?? is there any way to make that?
Hey Rodrigo,
Are you talking about Symfony HTTP cache here? Actually, Symfony do not make any automatic caching at all. All caching layers you have to add by yourself manually. So just do not add caching on specific route you won't to be cached and that's it. Or am I misunderstand you?
Cheers!
I dont really know where or what, i didnt set any cache at all, but in a production environment all my pages are working with cache, i did clear de cache and validate that the directory is empty, but after some random visits de cache directory has some new files and the content of my pages dosent have the latest information.
Hm, it's weird. Do you use shared hosting? Probably your hosting has some caching feature out-of-the-box, so you have to disable it first. Do you mean if you manually remove the "var/cache/prod/" on production - you still won't see the new updated (latest) HTML markup for example?
Cheers!
I am working with a shared hosting, they dont have any cache, if i delete all the files at "var/cache/prod" i see the new updated html, if after that i update the html and make a new visit to the same page with out clearing the cache i got the oldest one.
Hey Rodrigo,
Ah, that's on purpose - you have to clear the prod cache when you want to apply some changes in HTML layout. Actually, this way Symfony increase its performance what is important on production. So after each deploy, you have to clear the prod cache in console: "$ bin/console cache:clear -e prod" - if you have SSH access on your shared hosting, or just remove "var/cache/prod" directory manually - it should work as well. Symfony regenerate cache only for dev environment, which is very convenience for development - you don't have to clear the dev cache by itself. Btw, take a look at How to Deploy a Symfony Application in Symfony docs.
Cheers!
When you said "I bet we can figure out how to use this bundle on our own", I paused the video and went to the terminal to output the bundle reference and used config:dump-reference DoctrineCacheBundle, and it printed out the reference. Firstly, I love that I didn't have to search for the reference name and was able to use the bundle name instead as it saved a step. My question is -- is that by design? Can rely on bundle names to get me a proper reference dump?
Hey Michael!
Yes, you can rely on the bundle names like this! Internally, when you pass
DoctrineCacheBundle, Symfony looks for a Configuration file in that bundle, which provides the config information. And if you pass the config key instead -doctrine_cache- Symfony determines which bundle that config key is attached to (DoctrineCacheBundle) and then performs the same lookup for the Configuration file. So yes, passing the bundle name always works.Btw, if you're curious, here is the file Symfony looks for to load the configuration tree: https://github.com/doctrine/DoctrineCacheBundle/blob/master/DependencyInjection/Configuration.php
Cheers!
How to do this in symfony 3.4.5? In the lastest version of symfony3, the config.yml is not generated by default. I tried to load the file "doctrine_cache.yaml", but failed. When I type 'php ./bin/console dubug:container markdown_cache', the command line says 'In ContainerDebugCommand.php line 211: No services found that match "markdown_cache". Can you help me ?
Hey Nan,
You probably means how to do it in Symfony Flex. Looks like this bundle somehow does not a Symfony recipe. I also wonder why, so I opened an issue: https://github.com/doctrine/DoctrineCacheBundle/issues/131 . It's not a big deal, you can install this bundle with Composer and then manually create a config file for it in "config/packages/doctrine_cache.yaml" where paste related configuration, i.e.:
If you have other problems - let me know.
Cheers!
"Houston: no signs of life"
Start the conversation!