-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathstep-2.html
91 lines (89 loc) · 5.62 KB
/
step-2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
layout: tutorial-extended
title: The Weather App
---
{% assign tutorial_github_url = "https://github.com/infinispan/infinispan-embedded-tutorial" %}
{% assign @tutorial_steps = "The Weather Application, Initializing the CacheManager, Putting stuff in the cache, Making entries expire, Configuring the default cache, Clustering the application, Listen to changes in the cluster, Listen to changes in the cache, Grouping entries together, Temperature averages with streams, Declarative configuration" | split: ", " %}
{% assign tutorial_step = 2 %}
<div class="grid-wrapper">
<div class="width-3-12 width-12-12-m">
{% include embedded-tutorial-menu.html tutorial_steps=tutorial_steps tutorial_step=tutorial_step %}
</div>
<div class="width-9-12 width-12-12-m">
<h4>{{page.title}}</h4>
<p>
With a CacheManager running we can start creating caches and storing data in them. Caches in Infinispan are <i>"named"</i> which means they are identified by a unique name. We are going to enhance the <i>WeatherService</i> 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 <i>WeatherService</i> interface and the concrete implementations: the <i>CachingWeatherService.</i> 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:
</p>
<pre>
<code class="java">
cacheManager.defineConfiguration("weather", new ConfigurationBuilder().build());
Cache<String, LocationWeather> cache = cacheManager.getCache("weather");
</code>
</pre>
<p>
We are defining a named cache <i>"weather"</i> 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:
</p>
<pre>
<code class="java">
LocationWeather weather = cache.get(location);
if (weather == null) {
weather = fetchWeather(location);
cache.put(location, weather);
}
return weather;
</code>
</pre>
<p>
Since an Infinispan <i>Cache</i> is an implementation of the <i>java.util.Map</i> interface, the above code should be self explanatory. With the caching in place, let's run the application again, using the usual:
</p>
<pre>
<code class="nohighlight">
git checkout -f step-2
mvn clean package exec:exec
</code>
</pre>
<details>
<summary>Output</summary>
<div>
<pre>
<code class="nohighlight">
---- 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
---- Fetched in 2ms ----
</code>
</pre>
</div>
</details>
<p>
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.
</p>
</div>
</div>