Products, help and information

Cache system

Generally, there are two options to access data of the weather APIs. Without setting up a cache system, users access the meteoblue server directly when requesting data of the weather API (see variation 1). The customer server can easily set up a storage (cache system). This makes it possible that users do not access the meteoblue server directly, while still having access to the most recent weather data (see variation 2).

Variation 1 without cache system

Variation 1 without cache-system

In case no cache system is installed at the client, it has to be considered that:

  • Higher costs are generated with request-based pricing models (meteoblue server is accessed more often)
  • Information transfer can require more time

Variation 2 with cache system

Variation 2 with cache system

Setting up a cache system can be advantageous:

  • Reduction of operating costs of weather data supply as meteoblue server is accessed less often
  • Fast data transfer
  • Simple implementation
  • Transparency – constantly updated weather data is ensured

PHP example to implement a cache layer when using our API

We recommend using a cache layer to reduce the total amount of queries to our servers (which will result in lower costs for you) and improve latency. A popular and easy way to use cache is memcached:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/* Connect to memcached */
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

/* Try to get forecast from cache */
$url = "http://my.meteoblue.com/packages/basic-day/...&lat=47.5667&lon=7.6&asl=263"
$cache_key = md5($url);
if(($raw = $memcache->get($cache_key)) == false) {
    /* Cache returned an empty result */
    $raw = file_get_contents($url);
    $memcache->set($cache_key, $raw, MEMCACHE_COMPRESSED, 30*60);
}
$weather = json_decode($raw);

/* Print max temperature of today in Basel */
echo "Max temperature today in Basel: {$weather->data_day->temperature_max->0}";