koopjs / koop

Transform, query, and download geospatial data on the web.
http://koopjs.github.io
Other
660 stars 127 forks source link

Create unique cache keys for different layer indices #350

Closed sfabijanski closed 5 years ago

sfabijanski commented 5 years ago

While following suggestions in #302 to handle different datasets in the getData method for a provider by branching on different layer indices, I discovered the caching mechanism only allows one cache instance per provider. I dug in a little bit and in the createKey method in model.js I can see the cache key is derived by pulling the name of the provider based on the URL root from the provider such that any request pulls from a single cache despite a different layer ID.

I'm suggesting to add the layer ID to the cache key to enable multiple cache keys on a single provider. I'm not sure if handling of multiple layers is a supported aproach, but if so, something like this should allow multiple caches to be employed as well.

function createKey (req) {

  // append the layer id to the cache key
  var key = req.url.split('/')[1] + '_' + req.params.layer
  if (req.params.host) { key = [key, req.params.host].join('::') }
  if (req.params.id) { key = [key, req.params.id].join('::') }
  return key
}
rgwozdz commented 5 years ago

@sfabijanski - thanks for adding this. Cool idea. If you are up for submitting a PR to koop-core I would happily review.

Also note that every provider can define it's own createKey function to allow for customization of the keys. Simply add it to your Model.prototype as shown here and it will override the internal createKey function for that provider.

sfabijanski commented 5 years ago

@rgwozdz Thanks. I'll try to get a PR in place soon. I missed the email notification on your reply, else I would have responded sooner.

rgwozdz commented 5 years ago

Added and released in Koop 3.13.0.