Josiah
Josiah

Reputation: 727

Cache expiration with kml files and google maps

I have some dynamically generated KML files that I'd like to display on Google Maps. That's the easy part, I've done it a few times before, no problem.

What I want is for the KML files to only be cached by Google for a request. That is to say, if a person visits a page with a Google Maps embed, which has my KML loaded and displayed, I want that KML file to be used for that page load. But if the user were to refresh the page (having the same Google maps URL), I would like the KML file to be re-fetched.

The simple "just add a timestamp argument" doesn't work, as I'm not going to be in control of the urls passed to Google Maps on the page (just most of the url).

Looking at the <Link> and <networkLink> tags, they seem to let you control expiration, caching, refresh, etc., on an external resource. Reading the documentation, it would seem that using <refreshMode>onChange<refreshMode> to use a two-layered approach will get me what I want.

After testing this, Google isn't actually refreshing the content of the linked KML file, even after 10-20 minutes and multiple maps reloads (when I provide the same url to the Maps search box).

These are the KML files that I am using:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <NetworkLink>
    <Link>
      <href>URL</href>
      <refreshMode>onChange</refreshMode>
    </Link>
  </NetworkLink>
</Document>
</kml>

...

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
    <Placemark>
        <name>NAME</name>
        <visibility>1</visibility>
        <Style>
            STYLE
        </Style>
        GEOMETRY
    </Placemark>
</Document>
</kml>

... With URL, NAME, STYLE, and GEOMETRY replaced with correct content. Does anyone have an idea of what I might be doing wrong? Or if what I want is possible?

Upvotes: 4

Views: 5022

Answers (2)

Mano Marks
Mano Marks

Reputation: 8819

For expiration, Google follows the expiration time in the headers, but enforces a minimum expiration of 5 minutes to prevent 3rd party servers from getting hit with effective denial of services attacks.

Upvotes: 1

Josiah
Josiah

Reputation: 727

This is what I know:

  • onChange will cache the response completely unless you enable viewport passthrough on the KML loading via <viewFormat>, I've not seen any refresh after several hours.
  • onExpire with the use of a 'max-age' header on the linked KML file does not work in Google Maps (as per documentation), though I saw behavior described in [1], which could be related to a default internal Google Maps expire time.
  • onExpire with the use of <NetworkLinkControl> and <expires> sort-of works, in that the data seems to expire after the provided expiration time, though the behavior looked to be closer to [1]
  • onInterval will refresh the data, assuming your <viewRefreshTime> is sufficiently large, and will do it on its own schedule (the timing isn't reliable in the slightest) but the map view starts zoomed out completely (there doesn't seem to be a way to fix this).

[1] If you specify a 'max-age' of X seconds and you leave the page open, the KML will never refresh while the page has focus. If you move the focus to a different web page or application, then wait until some time well after the page should have refreshed (sometimes waiting 2 minutes is enough, sometimes it's not), and you go back to the page, it may make up to 30 requests in rapid succession to try to refresh the data if you don't return content, or just once if you do return content.

Long story short: cache control of KML files inside Google Maps with onExpire and onInterval is not reliable at all. At some point after your data is loaded, it will be refreshed, because Google's cache has gotten rid of it. But when does that cache expire? Some random time that is at least 5 minutes out.

Upvotes: 1

Related Questions