Route caching improves efficiency


Route caching

Because a layer of mapping is added, routing will lose efficiency. It is a good idea to cache routes

After defining a large number of routing rules, turning on the cache can significantly improve the routing resolution performance, and the more routing rules, the more obvious it is (valid only in deployment mode).

Set it up in the routing configuration file:

'route_check_cache'	=>	true,

After it is turned on, the schedule of each routing request will be automatically cached. If it is the same request the second time, there is no need to go through the routing again. parsing, but directly performs request scheduling.

By default, the routing cache identifier is the current request type and the BaseUrl containing the domain name. If your routing parameter detection is more complex, such as ajax/pjax detection, you need to customize the routing cache identifier. In the following way:

'route_check_cache_key'	=>	function($request) {
	return md5($request->url(true) . ':' . $request->method() . ':' . ($request->isAjax() ? 1 : 0));
},

Route cache supports all request types.

If you change the routing address of a routing rule, you need to clear the routing cache to take effect. You can use the following command to clear the routing cache.

>php think clear --route