perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.08k stars 350 forks source link

[suggestion] Caching routes #471

Closed sds-dubois closed 6 years ago

sds-dubois commented 6 years ago

First, thanks for this great plugin!

I noticed there is no option to cache the results of the routing API calls. This could be pretty useful in my opinion to avoid several unnecessary calls (and fees..) as well as delays, especially for applications where users are likely to toggle several times between a few path options.

I implemented this simply with a JSON object, by computing a unique key from the input waypoints and storing the route as the value, and checking this cache before calling the API.

I thought this could be easily integrated to the IRouter interface. Let me know if you that sounds interesting to you and maybe I can work on a PR.

perliedman commented 6 years ago

Hi, thanks for the suggestion, I can definitely see the use case for some applications.

For these types of things, I think a proxy router class is probably the best solution: that is, you can implement the IRouter interface to be a cache, and for queries not already in the cache, delegate to another IRouter, that will make the actual request to the backend.

The advantage of implementing it this way is it can be handled as a plugin for anyone who needs it, rather than an integrated part of Leaflet Routing Machine.

There are several other use cases that can, similarily, be implemented by using proxy router classes; for example, I've used this strategy to create a "round robing router", such that the route always returns to the starting point.

If you would implement something like this, I'd be happy to link to it from the Leaflet Routing Machine page so other users can easily find it!

perliedman commented 6 years ago

Closing this since it does not look like there is anything happening here.