Search code examples
restcachinggeolocationlocationhttp-caching

Caching Strategy for location requests


I am building REST APIs that return data (lets say events ) in particular area. The REST URL is a simple GET

/api/v1/events?lat=<lat>&lng=<lng>&radius=<radius>.

with parameters lat, lng and radius (10 miles by default), the latitude and longitude are what the device or browser APIs return. Now needless to say that the lat and lng change continuously as the user moves and also two users can be same vicinity with different lat / lng. What is the best way to cache such kind of requests on the server so that I don't have to dip into business logic everytime. The URL is not going to unique since lat/lng change.

Thanks


Solution

  • I'm assuming you have some sort of "grid", and when a user requests a specific coordinate, you return the grid tile(s) around the location. So you have an infinite URL space (coordinates) that is mapped to a finite number of tiles. One solution is to redirect every request to the "canonical", cache friendly URL for that tile, e.g.

    GET /api/v1/events?lat=123&lng=456
    =>
    302 Found
    Location: /api/v1/events?tile=abc
    

    Or, if you want to retain the lat/long info in the URL, you could use the location of the center of the tile.