GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Caching pattern: Implementation of Cache-Aside pattern · coding0011/java-design-patterns@b31edda

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • caching/src
    • main/java/com/iluwatar/caching
      • App.java
      • AppManager.java
      • CacheStore.java
      • CachingPolicy.java
    • test/java/com/iluwatar/caching
      • CachingTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/App.java
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public static void main(String[] args) {
7474
app.useReadAndWriteThroughStrategy();
7575
app.useReadThroughAndWriteAroundStrategy();
7676
app.useReadThroughAndWriteBehindStrategy();
77+
app.useCacheAsideStategy();
7778
}
7879

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

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

Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/AppManager.java
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public static UserAccount find(String userId) {
8181
return CacheStore.readThrough(userId);
8282
} else if (cachingPolicy == CachingPolicy.BEHIND) {
8383
return CacheStore.readThroughWithWriteBackPolicy(userId);
84+
} else if (cachingPolicy == CachingPolicy.ASIDE) {
85+
return findAside(userId);
8486
}
8587
return null;
8688
}
@@ -95,10 +97,37 @@ public static void save(UserAccount userAccount) {
9597
CacheStore.writeAround(userAccount);
9698
} else if (cachingPolicy == CachingPolicy.BEHIND) {
9799
CacheStore.writeBehind(userAccount);
100+
} else if (cachingPolicy == CachingPolicy.ASIDE) {
101+
saveAside(userAccount);
98102
}
99103
}
100104

101105
public static String printCacheContent() {
102106
return CacheStore.print();
103107
}
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+
}
104133
}
Collapse file

‎caching/src/main/java/com/iluwatar/caching/CacheStore.java‎

Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/CacheStore.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,25 @@ public static String print() {
153153
sb.append("----\n");
154154
return sb.toString();
155155
}
156+
157+
/**
158+
* Delegate to backing cache store
159+
*/
160+
public static UserAccount get(String userId) {
161+
return cache.get(userId);
162+
}
163+
164+
/**
165+
* Delegate to backing cache store
166+
*/
167+
public static void set(String userId, UserAccount userAccount) {
168+
cache.set(userId, userAccount);
169+
}
170+
171+
/**
172+
* Delegate to backing cache store
173+
*/
174+
public static void invalidate(String userId) {
175+
cache.invalidate(userId);
176+
}
156177
}
Collapse file

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

Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/CachingPolicy.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
*/
3030
public enum CachingPolicy {
31-
THROUGH("through"), AROUND("around"), BEHIND("behind");
31+
THROUGH("through"), AROUND("around"), BEHIND("behind"), ASIDE("aside");
3232

3333
private String policy;
3434

Collapse file

‎caching/src/test/java/com/iluwatar/caching/CachingTest.java‎

Copy file name to clipboardExpand all lines: caching/src/test/java/com/iluwatar/caching/CachingTest.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ public void testReadThroughAndWriteAroundStrategy() {
6060
public void testReadThroughAndWriteBehindStrategy() {
6161
app.useReadThroughAndWriteBehindStrategy();
6262
}
63+
64+
@Test
65+
public void testCacheAsideStrategy() {
66+
app.useCacheAsideStategy();
67+
}
6368
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.