Skip to content

Commit 64bdab4

Browse files
authored
Merge pull request #495 from christofferh/feat/cache-aside
Caching pattern: Cache-Aside
2 parents 4ca205c + f114b5b commit 64bdab4

File tree

11 files changed

+192
-137
lines changed

11 files changed

+192
-137
lines changed

caching/README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 87 additions & 103 deletions
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

Lines changed: 35 additions & 11 deletions
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
@@ -74,6 +75,7 @@ public static void main(String[] args) {
7475
app.useReadAndWriteThroughStrategy();
7576
app.useReadThroughAndWriteAroundStrategy();
7677
app.useReadThroughAndWriteBehindStrategy();
78+
app.useCacheAsideStategy();
7779
}
7880

7981
/**
@@ -136,4 +138,26 @@ public void useReadThroughAndWriteBehindStrategy() {
136138
AppManager.find("004");
137139
System.out.println(AppManager.printCacheContent());
138140
}
141+
142+
/**
143+
* Cache-Aside
144+
*/
145+
public void useCacheAsideStategy() {
146+
System.out.println("# CachingPolicy.ASIDE");
147+
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
148+
System.out.println(AppManager.printCacheContent());
149+
150+
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
151+
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
152+
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
153+
AppManager.save(userAccount3);
154+
AppManager.save(userAccount4);
155+
AppManager.save(userAccount5);
156+
157+
System.out.println(AppManager.printCacheContent());
158+
AppManager.find("003");
159+
System.out.println(AppManager.printCacheContent());
160+
AppManager.find("004");
161+
System.out.println(AppManager.printCacheContent());
162+
}
139163
}

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ public static void initDb(boolean useMongoDb) {
6464
public static void initCachingPolicy(CachingPolicy policy) {
6565
cachingPolicy = policy;
6666
if (cachingPolicy == CachingPolicy.BEHIND) {
67-
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
68-
@Override
69-
public void run() {
70-
CacheStore.flushCache();
71-
}
72-
}));
67+
Runtime.getRuntime().addShutdownHook(new Thread(CacheStore::flushCache));
7368
}
7469
CacheStore.clearCache();
7570
}
@@ -86,6 +81,8 @@ public static UserAccount find(String userId) {
8681
return CacheStore.readThrough(userId);
8782
} else if (cachingPolicy == CachingPolicy.BEHIND) {
8883
return CacheStore.readThroughWithWriteBackPolicy(userId);
84+
} else if (cachingPolicy == CachingPolicy.ASIDE) {
85+
return findAside(userId);
8986
}
9087
return null;
9188
}
@@ -100,10 +97,37 @@ public static void save(UserAccount userAccount) {
10097
CacheStore.writeAround(userAccount);
10198
} else if (cachingPolicy == CachingPolicy.BEHIND) {
10299
CacheStore.writeBehind(userAccount);
100+
} else if (cachingPolicy == CachingPolicy.ASIDE) {
101+
saveAside(userAccount);
103102
}
104103
}
105104

106105
public static String printCacheContent() {
107106
return CacheStore.print();
108107
}
108+
109+
/**
110+
* Cache-Aside save user account helper
111+
*/
112+
private static void saveAside(UserAccount userAccount) {
113+
DbManager.updateDb(userAccount);
114+
CacheStore.invalidate(userAccount.getUserId());
115+
}
116+
117+
/**
118+
* Cache-Aside find user account helper
119+
*/
120+
private static UserAccount findAside(String userId) {
121+
UserAccount userAccount = CacheStore.get(userId);
122+
if (userAccount != null) {
123+
return userAccount;
124+
}
125+
126+
userAccount = DbManager.readFromDb(userId);
127+
if (userAccount != null) {
128+
CacheStore.set(userId, userAccount);
129+
}
130+
131+
return userAccount;
132+
}
109133
}

0 commit comments

Comments
 (0)