Tutorials The Weather App

The Weather App

With a CacheManager running we can start creating caches and storing data in them. Caches in Infinispan are "named" which means they are identified by a unique name. We are going to enhance the WeatherService implementations from our initial version so that they store the retrieved data in a cache. To do this we will create an intermediate abstract class between the WeatherService interface and the concrete implementations: the CachingWeatherService. This class wraps the expensive remote calls in a method which caches the result returned by the remote call and, before subsequent invocations, checks whether the data is already available locally instead of invoking the expensive remote API. First of all let's obtain our cache:

        
= preserve do
  :escaped
    cacheManager.defineConfiguration("weather", new ConfigurationBuilder().build());
    Cache cache = cacheManager.getCache("weather");
        
      
We are defining a named cache "weather" using a default configuration, which means a basic local cache. As you can see, the generic types of the keys and values are inferred from the assignment type. Next up, let's see the caching wrapper for the weather retrieval:
        
          = preserve do
            :escaped
              LocationWeather weather = cache.get(location);
              if (weather == null) {
                weather = fetchWeather(location);
                cache.put(location, weather);
              }
              return weather;
        
      
Since an Infinispan Cache is an implementation of the java.util.Map interface, the above code should be self explanatory. With the caching in place, let's run the application again, using the usual:
        
          = preserve do
            :escaped
              $ git checkout -f step-2
              $ mvn clean package exec:exec
        
      
Output
              
                = preserve do
                  :escaped
                    ---- Fetching weather information ----
                    Rome, Italy - Temperature: 12.9° C, Conditions: light rain
                    Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
                    Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
                    Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
                    London, UK - Temperature: 1.8° C, Conditions: light rain
                    Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
                    Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
                    Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
                    Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
                    Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
                    Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
                    Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
                    Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
                    Washington, USA - Temperature: 3.4° C, Conditions: light rain
                    ---- Fetched in 1634ms ----
                    ---- Fetching weather information ----
                    Rome, Italy - Temperature: 12.9° C, Conditions: light rain
                    Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
                    Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
                    Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
                    London, UK - Temperature: 1.8° C, Conditions: light rain
                    Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
                    Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
                    Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
                    Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
                    Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
                    Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
                    Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
                    Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
                    Washington, USA - Temperature: 3.4° C, Conditions: light rain
                    ---- Fetched in 2ms ----
              
            

You should see the weather conditions displayed twice, but the second run should be several orders of magnitude faster. Good stuff! Entries created above are immortal, i.e. they will exist in the cache until removed. In the next step we will see how to add mortal entries.