What is a Weather Cache?
The firstworks Weather Cache is a light, simple, caching proxy, targeted for use with Weather API's.
Weather API's provide web-services that allow you to display current conditions, forecasts or historical weather information on your website or mobile app. For example:
But there are several issues with these innocuous-looking badges.
Downloading weather data from across the internet takes time. Fetching it on the server-side while building a page can slow page loads significantly.
You could update your site so that the data is fetched on the client-side after the rest of the page has loaded but that introduces Cross-Site Scripting (XSS) issues. Yes, you can work around these issues with JSONP and CORS, but only if the Weather API supports them.
But the bigger issue, the one that crops up when your site starts getting popular, is request count limits. When you exceed some number of requests during some period of time you get throttled, blocked, or have to pay more.
The firstworks Weather Cache sits between your application and the Weather API's web-service, forwarding requests from your application and receiving results from the web-service. Each result is cached in a local database and tagged with an expiration date before being returned to your application. If the cache receives a request for the same data prior to the expiration date, the result is fetched from the database, rather than from the web-service.
- When results are fetched from the local database rather than from across the internet, page load times are improved dramatically.
- Since duplicate requests are resolved locally, the number of requests to the Weather API are minimized.
- Cross-Site Scripting issues are avoided because the domain of the cache is the same as the rest of your site.
The firstworks Weather Cache is extremely simple. It's distributed as a self-contained, statically-linked x86 Linux binary and should be compatible with any x86 or x86_64 Linux platform. Its only requirements are a web server configured to support CGI's and read/write access to the /var/tmp directory. Simply install it and aim it at the Weather API of your choice:
The Weather Cache is designed to be API-agnostic but it does a few semi-intelligent things to work around some common cache-defeating issues.
- Parameters are normalized so subtly different requests for the same information will be recognized as the same request.
- Mobile devices are "snapped" to a grid. Successive requests from mobile devices tend to send slightly different latitudes and longitudes, especially if the device is moving. When snapped to a grid, the location of the device only changes when it has has moved far enough to snap to a different grid point. Multiple mobile devices in close proximity snap to the same grid point.