-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Documentation for the new PSR-6 Cache component #6171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I love the energy and passion that the Symfony community is showing around the new Cache component. I'd like to start documenting it and adding it to symfony.com list of components. But before that, I'd like to share with you how I'd like this new cache to work in Symfony. I'd like to have a "global" and standard way of using a cache for services and bundles. Similar to the way we use Monolog today for logging anything. So, my question: is the following explanation how the Cache would work in Symfony? Defining application cachesWhen defining one cache, Symfony names it # app/config/config.yml
# simple cache without arguments
cache:
type: array
# equivalent to:
# cache:
# default:
# type: array Complex caches can define arguments: # app/config/config.yml
cache:
type: doctrine
em: my_custom_em_id You can also define lots of different caches in a single app: # app/config/config.yml
cache:
default:
type: array
db:
type: doctrine
em: my_custom_em_id They are available in the application as Configuring the application cachesParams such as # app/config/config.yml
cache:
type: array
default_lifetime: 600
namespace: 'ACME__' Using caches in Symfony componentsAll Symfony components define a
# app/config/config.yml
framework:
validation:
cache: true
enable_annotations: true
serializer:
cache: true
property_path:
cache: true
doctrine:
orm:
cache: true
# you can also set 'metadata_cache_driver', 'result_cache_driver'
# and 'query_cache_driver' separately You can also set the # app/config/config.yml
framework:
validation:
cache: db
enable_annotations: true
serializer:
cache: default
property_path:
cache: default
doctrine:
orm:
cache: [db, default] This cache can also be used for third-party bundles: # app/config/config.yml
liip_imagine:
cache: true # or false, or 'db', or ['db', 'default'], etc. |
What you are mentioning is not Component docs, but bundle docs (cookbook related), @javiereguiluz. Apart from that, I cannot answer your questions as I haven't looked at caching yet |
@javiereguiluz Your ideas sound quite nice to me and I think you should raise this idea in the code repo (we are not there right now, but the work to integrate PSR-6 caches in the different components has started). Some of the things you talk about are out of the scope of the Symfony core as they need to be handled in third-party bundles, but we should of course think about how can make using the new cache component as easy as possible. |
For anyone interested: Javier opened symfony/symfony#17537. |
Is there any documentation for symfony/symfony#18371 (also symfony/symfony#18544 and symfony/symfony#18561 which introduced default cache pools for the Validator and Serializer)? How would the user switch to use The recommended (as per |
@teohhanhui You are right, these PR should be documented. However, note that these three PR do not change any behavior when the user enabled APC cache: these PR are simply a fallback to filesystem when the user didn't change the config_prod file. The recommended cache will also need to change from APC to OPCache as much as possible. That's the aim of the PR symfony/symfony#18533 and there will be other PRs for other components (validator and serializer mainly). PS : the cache pools system definitely needs some documentation, it's a really powerful system but more complex than what was present before. |
This comment could be a good basis for documentation: symfony/symfony#18371 (comment) |
An educated guess from the code says that I can safely do this: # app/config_prod.yml
framework:
cache:
pools:
serializer:
adapter: cache.adapter.apcu Am I right? and |
If so, then shouldn't that be the new recommendation in p/s: OpCacheAdapter or ApcuAdapter, that's a separate issue... |
@teohhanhui Your configuration sample is right, but as it's not completely stable yet (some ideas emerged to improve developer experience on various levels), it's not yet documented. But it should be before release, that's true. |
I'm trying to write the docs for this component (and its integration with FrameworkBundle). So I'll ask for help to @tgalopin and @nicolas-grekas to see if I understand their work. In the Symfony Demo app, we have this action to display the blog index: public function indexAction($page)
{
$posts = $this->get('cache.pool.local')->getItem('blog-posts-latest');
return $this->render('blog/index.html.twig', array('posts' => $posts));
} I'm trying to use a filesystem cache to avoid the database query: public function indexAction($page)
{
if ($this->get('cache.pool.local')->hasItem('blog-posts-latest')) {
$posts = $this->get('cache.pool.local')->getItem('blog-posts-latest');
} else {
$posts = $this->getDoctrine()->getRepository('AppBundle:Post')->findLatest($page);
$item = $this->get('cache.pool.local')->getItem('blog-posts-latest');
$item->set($posts);
$this->get('cache.pool.local')->save($item);
}
return $this->render('blog/index.html.twig', array('posts' => $posts));
} The "pools" are created, but no contents are cached. Everything is empty: Any clue? Thanks! |
You should call // wrong!
$posts = $this->get('cache.pool.local')->getItem('blog-posts-latest'); It's unsafe to use the Working example: https://github.com/api-platform/core/blob/b655b91f83e2acb4040767dab51452512638e45c/src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php |
@teohhanhui I've changed the code like this, but everything is still empty: public function indexAction($page)
{
$cachedPosts = $this->get('cache.pool.local')->getItem('blog-posts-latest');
if ($cachedPosts->isHit()) {
$posts = $cachedPosts->get();
} else {
$posts = $this->getDoctrine()->getRepository('AppBundle:Post')->findLatest($page);
$cachedPosts->set($posts);
$this->get('cache.pool.local')->save($cachedPosts);
}
return $this->render('blog/index.html.twig', array('posts' => $posts));
} |
@teohhanhui OK, the problem was that I was trying to cache a |
@javiereguiluz PSR-6 forbids throwing exceptions for unserializable values. But we could easily add a logger, only a service tag is missing. In which channel would you do that? |
What about using |
Need to document symfony/symfony#18667 |
@teohhanhui yes, but I'll do that in a separate PR. This is only for the component and then we'll document the integration of Cache in the Symfony framework. |
@javiereguiluz I am confused, where can I find complete description of cache component configuration in symfony framework? There is nothing about it in symfony docs website. |
@Arkemlar sadly that's correct! We don't have yet this doc about using Cache inside the Symfony framework. |
Reopening, the framework integration is indeed still missing as stated in the original post. |
Closing again as we already track it in #7006. |
The text was updated successfully, but these errors were encountered: