Skip to content

Commit f114b5b

Browse files
committed
Caching pattern: Documentation and diagram
1 parent b31edda commit f114b5b

File tree

7 files changed

+101
-120
lines changed

7 files changed

+101
-120
lines changed

caching/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Use the Caching pattern(s) when
2727

2828
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
2929
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
30+
* [Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)

caching/etc/caching.png

57.2 KB
Loading

caching/etc/caching.ucls

+87-103
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,90 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3-
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4-
<class id="1" language="java" name="main.java.com.wssia.caching.App" project="CachingPatterns"
5-
file="/CachingPatterns/src/main/java/com/wssia/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="-1" width="-1" x="249" y="150"/>
7-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8-
sort-features="false" accessors="true" visibility="true">
9-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10-
<operations public="true" package="true" protected="true" private="true" static="true"/>
11-
</display>
2+
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.caching.CacheStore" project="caching"
6+
file="/caching/src/main/java/com/iluwatar/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="322" y="444"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.caching.LruCache" project="caching"
15+
file="/caching/src/main/java/com/iluwatar/caching/LruCache.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="700" y="446"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</class>
23+
<enumeration id="3" language="java" name="com.iluwatar.caching.CachingPolicy" project="caching"
24+
file="/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
25+
<position height="-1" width="-1" x="943" y="122"/>
26+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
27+
sort-features="false" accessors="true" visibility="true">
28+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
29+
<operations public="true" package="true" protected="true" private="true" static="true"/>
30+
</display>
31+
</enumeration>
32+
<class id="4" language="java" name="com.iluwatar.caching.DbManager" project="caching"
33+
file="/caching/src/main/java/com/iluwatar/caching/DbManager.java" binary="false" corner="BOTTOM_RIGHT">
34+
<position height="-1" width="-1" x="1011" y="426"/>
35+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
36+
sort-features="false" accessors="true" visibility="true">
37+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
38+
<operations public="true" package="true" protected="true" private="true" static="true"/>
39+
</display>
40+
</class>
41+
<class id="5" language="java" name="com.iluwatar.caching.App" project="caching"
42+
file="/caching/src/main/java/com/iluwatar/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
43+
<position height="-1" width="-1" x="304" y="95"/>
44+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
45+
sort-features="false" accessors="true" visibility="true">
46+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
47+
<operations public="true" package="true" protected="true" private="true" static="true"/>
48+
</display>
49+
</class>
50+
<class id="6" language="java" name="com.iluwatar.caching.AppManager" project="caching"
51+
file="/caching/src/main/java/com/iluwatar/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
52+
<position height="-1" width="-1" x="604" y="122"/>
53+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
54+
sort-features="false" accessors="true" visibility="true">
55+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
56+
<operations public="true" package="true" protected="true" private="true" static="true"/>
57+
</display>
58+
</class>
59+
<class id="7" language="java" name="com.iluwatar.caching.UserAccount" project="caching"
60+
file="/caching/src/main/java/com/iluwatar/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
61+
<position height="-1" width="-1" x="1161" y="139"/>
62+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
63+
sort-features="false" accessors="true" visibility="true">
64+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
65+
<operations public="true" package="true" protected="true" private="true" static="true"/>
66+
</display>
1267
</class>
13-
<class id="2" language="java" name="main.java.com.wssia.caching.AppManager" project="CachingPatterns"
14-
file="/CachingPatterns/src/main/java/com/wssia/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="-1" width="-1" x="502" y="163"/>
16-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17-
sort-features="false" accessors="true" visibility="true">
18-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19-
<operations public="true" package="true" protected="true" private="true" static="true"/>
20-
</display>
21-
</class>
22-
<class id="3" language="java" name="main.java.com.wssia.caching.CacheStore" project="CachingPatterns"
23-
file="/CachingPatterns/src/main/java/com/wssia/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="-1" width="-1" x="537" y="436"/>
25-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26-
sort-features="false" accessors="true" visibility="true">
27-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28-
<operations public="true" package="true" protected="true" private="true" static="true"/>
29-
</display>
30-
</class>
31-
<enumeration id="4" language="java" name="main.java.com.wssia.caching.CachingPolicy" project="CachingPatterns"
32-
file="/CachingPatterns/src/main/java/com/wssia/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="-1" width="-1" x="789" y="162"/>
34-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35-
sort-features="false" accessors="true" visibility="true">
36-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37-
<operations public="true" package="true" protected="true" private="true" static="true"/>
38-
</display>
39-
</enumeration>
40-
<class id="5" language="java" name="main.java.com.wssia.caching.DBManager" project="CachingPatterns"
41-
file="/CachingPatterns/src/main/java/com/wssia/caching/DBManager.java" binary="false" corner="BOTTOM_RIGHT">
42-
<position height="-1" width="-1" x="1137" y="134"/>
43-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44-
sort-features="false" accessors="true" visibility="true">
45-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46-
<operations public="true" package="true" protected="true" private="true" static="true"/>
47-
</display>
48-
</class>
49-
<class id="6" language="java" name="main.java.com.wssia.caching.LRUCache" project="CachingPatterns"
50-
file="/CachingPatterns/src/main/java/com/wssia/caching/LRUCache.java" binary="false" corner="BOTTOM_RIGHT">
51-
<position height="-1" width="-1" x="884" y="435"/>
52-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
53-
sort-features="false" accessors="true" visibility="true">
54-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
55-
<operations public="true" package="true" protected="true" private="true" static="true"/>
56-
</display>
57-
</class>
58-
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
59-
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
60-
<position height="-1" width="-1" x="1140" y="405"/>
61-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
62-
sort-features="false" accessors="true" visibility="true">
63-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
64-
<operations public="true" package="true" protected="true" private="true" static="true"/>
65-
</display>
66-
</class>
67-
<class id="8" language="java" name="test.java.com.wssia.caching.AppTest" project="CachingPatterns"
68-
file="/CachingPatterns/src/test/java/com/wssia/caching/AppTest.java" binary="false" corner="BOTTOM_RIGHT">
69-
<position height="-1" width="-1" x="251" y="374"/>
70-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
71-
sort-features="false" accessors="true" visibility="true">
72-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
73-
<operations public="true" package="true" protected="true" private="true" static="true"/>
74-
</display>
75-
</class>
76-
<association id="9">
77-
<end type="SOURCE" refId="2" navigable="false">
78-
<attribute id="10" name="cachingPolicy"/>
79-
<multiplicity id="11" minimum="0" maximum="1"/>
80-
</end>
81-
<end type="TARGET" refId="4" navigable="true"/>
82-
<display labels="true" multiplicity="true"/>
83-
</association>
84-
<association id="12">
85-
<end type="SOURCE" refId="8" navigable="false">
86-
<attribute id="13" name="app"/>
87-
<multiplicity id="14" minimum="0" maximum="1"/>
88-
</end>
89-
<end type="TARGET" refId="1" navigable="true"/>
90-
<display labels="true" multiplicity="true"/>
91-
</association>
92-
<association id="15">
93-
<end type="SOURCE" refId="3" navigable="false">
94-
<attribute id="16" name="cache"/>
95-
<multiplicity id="17" minimum="0" maximum="1"/>
96-
</end>
97-
<end type="TARGET" refId="6" navigable="true"/>
98-
<display labels="true" multiplicity="true"/>
99-
</association>
100-
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
101-
sort-features="false" accessors="true" visibility="true">
102-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
103-
<operations public="true" package="true" protected="true" private="true" static="true"/>
104-
</classifier-display>
68+
<association id="8">
69+
<end type="SOURCE" refId="1" navigable="false">
70+
<attribute id="9" name="cache"/>
71+
<multiplicity id="10" minimum="0" maximum="1"/>
72+
</end>
73+
<end type="TARGET" refId="2" navigable="true"/>
74+
<display labels="true" multiplicity="true"/>
75+
</association>
76+
<association id="11">
77+
<end type="SOURCE" refId="6" navigable="false">
78+
<attribute id="12" name="cachingPolicy"/>
79+
<multiplicity id="13" minimum="0" maximum="1"/>
80+
</end>
81+
<end type="TARGET" refId="3" navigable="true"/>
82+
<display labels="true" multiplicity="true"/>
83+
</association>
84+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
85+
sort-features="false" accessors="true" visibility="true">
86+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
87+
<operations public="true" package="true" protected="true" private="true" static="true"/>
88+
</classifier-display>
10589
<association-display labels="true" multiplicity="true"/>
106-
</class-diagram>
90+
</class-diagram>

caching/src/main/java/com/iluwatar/caching/App.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,23 @@
2626
*
2727
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
2828
* the resources immediately after their use. The resources retain their identity, are kept in some
29-
* fast-access storage, and are re-used to avoid having to acquire them again. There are three main
30-
* caching strategies/techniques in this pattern; each with their own pros and cons. They are:
29+
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
30+
* caching strategies/techniques in this pattern; each with their own pros and cons. They are;
3131
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
32-
* <code>write-around</code> which writes data immediately into the DB instead of the cache, and
32+
* <code>write-around</code> which writes data immediately into the DB instead of the cache,
3333
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
34-
* written into the DB when the cache is full. The <code>read-through</code> strategy is also
35-
* included in the mentioned three strategies -- returns data from the cache to the caller <b>if</b>
36-
* it exists <b>else</b> queries from DB and stores it into the cache for future use. These
37-
* strategies determine when the data in the cache should be written back to the backing store (i.e.
38-
* Database) and help keep both data sources synchronized/up-to-date. This pattern can improve
39-
* performance and also helps to maintain consistency between data held in the cache and the data in
40-
* the underlying data store.
34+
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
35+
* responsibility of keeping the data synchronized in both data sources to the application itself.
36+
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
37+
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
38+
* stores it into the cache for future use. These strategies determine when the data in the cache
39+
* should be written back to the backing store (i.e. Database) and help keep both data sources
40+
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
41+
* consistency between data held in the cache and the data in the underlying data store.
4142
* <p>
4243
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
4344
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
44-
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
45+
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
4546
* strategies are individually tested. The testing of the cache is restricted towards saving and
4647
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
4748
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and

caching/src/main/java/com/iluwatar/caching/CachingPolicy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/**
2626
*
27-
* Enum class containing the three caching strategies implemented in the pattern.
27+
* Enum class containing the four caching strategies implemented in the pattern.
2828
*
2929
*/
3030
public enum CachingPolicy {

caching/src/main/java/com/iluwatar/caching/LruCache.java

-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public UserAccount get(String userId) {
7373
}
7474

7575
/**
76-
*
7776
* Remove node from linked list.
7877
*/
7978
public void remove(Node node) {
@@ -90,7 +89,6 @@ public void remove(Node node) {
9089
}
9190

9291
/**
93-
*
9492
* Move node to the front of the list.
9593
*/
9694
public void setHead(Node node) {
@@ -161,7 +159,6 @@ public void clear() {
161159
}
162160

163161
/**
164-
*
165162
* Returns cache data in list form.
166163
*/
167164
public List<UserAccount> getCacheDataInListForm() {

caching/src/main/java/com/iluwatar/caching/UserAccount.java

-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
package com.iluwatar.caching;
2424

2525
/**
26-
*
2726
* Entity class (stored in cache and DB) used in the application.
28-
*
2927
*/
3028
public class UserAccount {
3129
private String userId;

0 commit comments

Comments
 (0)