How to cache all routes to one file?

I have a lot of custom routes,and many many routes files.

For example:

So,my question is How to cache all routes to one file?

If the router cache file exist,that will be load and set to the DI?How to do?

Now ,i hava get all routes form code $di->get('route')->getRoutes(),but i dont kown how to set to the di?

Use $di->getShared() insteand of get to avoid create a new instance on each call. Then you can implement a cache for routes

Well you can even just serialize whole router object if you don't use and closures, using just file cache if you have really many routes can speed application signfically.

I'm curious why you have so many defined routes? By default Phalcon is configured to handle routes of the format /controller/action/parameter, so anything in that format shouldn't need to be re-defined.

edited 18d ago

Use $di->getShared() insteand of get to avoid create a new instance on each call. Then you can implement a cache for routes

I know this!But my question is not this,my question is every http request that app reload a lot of router file!

Unfortunately,closure cant be serialize!

Well you can even just serialize whole router object if you don't use and closures, using just file cache if you have really many routes can speed application signfically.