How to update a specific cached route?

I am running a Symfony2 application and I have a question about caching.

There is a comment on the answer in SO that says:

you can create a team that updates only this cached route. or perhaps consider using a kernel event listener that registers a route for each request if you can afford the performance impact.

How can I update only this cached route?

+1
source share
2 answers

Where are cached routes stored?

Cache classes for mapping / generating URLs can be found in app/cache/environment and are called appEnvironmentUrlGenerator.php and appEnvironmentUrlGenerator.php , and the "environment" is one of dev , prod , etc.

API Link:

http://api.symfony.com/2.3/Symfony/Component/Routing/Matcher/UrlMatcher.html http://api.symfony.com/2.3/Symfony/Component/Routing/Generator/UrlGenerator.html

How it works?

When creating a router, the router service receives a url-matcher and a url-generator. They are then used inside the match() and generate() methods of the router.

https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Router.php

To warm the cache, RoutingCacheWarmer uses the router warmUp() method (if it implements WarmableInterface ).

+2
source

Everything written by nifr is true, but it really doesn't help you.
Symfony is built in a router designed to support β€œstatic” routes, so when the cache is first warmed up, routes will be generated and cached. If you check the specified cache files, you will see that the routes are stored in a static private variable.

There is no easy way to update a route (or change routes).

If you check the CacheClearCommand , you will see that the implementation is quite complicated. Some suggest deleting the entire cache directory, which I do not recommend, it is quite heavy and causes your site to freeze until the cache is regenerated (even you can get exceptions for missing files / and logged out users if sessions stored in the folder with cache)

Calling cache leak mode is not possible if the cache is already delegated.

If you just delete the two related cache files and then call warmUp on the router, this would be a better solution, but also not a pleasant one.

  $cacheDir = $this->container->getParameter("kernel.cache_dir"); // Delete routing cache files $filesystem = $this->container->get('filesystem'); $finder = new Finder(); /** @var File $file */ foreach ($finder->files()->depth('== 0')->in($cacheDir) as $file) { if (preg_match('/UrlGenerator|UrlMatcher/', $file->getFilename()) == 1) { $filesystem->remove($file->getRealpath()); } } $router = $this->container->get('router'); $router->warmUp($cacheDir); 
  • Override the default router class

Starting from 2.8, the Router class is defined by the router.class parameter . See here: https://github.com/symfony/framework-bundle/blob/v2.8.2/Resources/config/routing.xml#L63

Add something like this to your config.yml

parameters: router.class: "My \ Fancy \ Router"

You can implement your own router class by extending the source router, and you also need to extend the UrlMatcher and UrlGenerator classes to call the parent implementation and add your own routes to map to / generate. This way you do not need to update the cache, as you can dynamically add your routes.

Note. I'm not sure that you can rely on this in the long run, if you check the wizard, the definition has changed, the parameter no longer exists:

https://github.com/symfony/framework-bundle/blob/master/Resources/config/routing.xml#L54

+2
source

Source: https://habr.com/ru/post/1502016/


All Articles