Caching pattern: Implementation of Cache-Aside pattern

This commit is contained in:
Christoffer Hamberg 2016-09-22 20:41:26 +02:00
parent e3355d76d1
commit b31edda3cf
5 changed files with 79 additions and 1 deletions

View File

@ -74,6 +74,7 @@ public class App {
app.useReadAndWriteThroughStrategy();
app.useReadThroughAndWriteAroundStrategy();
app.useReadThroughAndWriteBehindStrategy();
app.useCacheAsideStategy();
}
/**
@ -136,4 +137,26 @@ public class App {
AppManager.find("004");
System.out.println(AppManager.printCacheContent());
}
/**
* Cache-Aside
*/
public void useCacheAsideStategy() {
System.out.println("# CachingPolicy.ASIDE");
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
System.out.println(AppManager.printCacheContent());
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
AppManager.save(userAccount3);
AppManager.save(userAccount4);
AppManager.save(userAccount5);
System.out.println(AppManager.printCacheContent());
AppManager.find("003");
System.out.println(AppManager.printCacheContent());
AppManager.find("004");
System.out.println(AppManager.printCacheContent());
}
}

View File

@ -81,6 +81,8 @@ public final class AppManager {
return CacheStore.readThrough(userId);
} else if (cachingPolicy == CachingPolicy.BEHIND) {
return CacheStore.readThroughWithWriteBackPolicy(userId);
} else if (cachingPolicy == CachingPolicy.ASIDE) {
return findAside(userId);
}
return null;
}
@ -95,10 +97,37 @@ public final class AppManager {
CacheStore.writeAround(userAccount);
} else if (cachingPolicy == CachingPolicy.BEHIND) {
CacheStore.writeBehind(userAccount);
} else if (cachingPolicy == CachingPolicy.ASIDE) {
saveAside(userAccount);
}
}
public static String printCacheContent() {
return CacheStore.print();
}
/**
* Cache-Aside save user account helper
*/
private static void saveAside(UserAccount userAccount) {
DbManager.updateDb(userAccount);
CacheStore.invalidate(userAccount.getUserId());
}
/**
* Cache-Aside find user account helper
*/
private static UserAccount findAside(String userId) {
UserAccount userAccount = CacheStore.get(userId);
if (userAccount != null) {
return userAccount;
}
userAccount = DbManager.readFromDb(userId);
if (userAccount != null) {
CacheStore.set(userId, userAccount);
}
return userAccount;
}
}

View File

@ -153,4 +153,25 @@ public class CacheStore {
sb.append("----\n");
return sb.toString();
}
/**
* Delegate to backing cache store
*/
public static UserAccount get(String userId) {
return cache.get(userId);
}
/**
* Delegate to backing cache store
*/
public static void set(String userId, UserAccount userAccount) {
cache.set(userId, userAccount);
}
/**
* Delegate to backing cache store
*/
public static void invalidate(String userId) {
cache.invalidate(userId);
}
}

View File

@ -28,7 +28,7 @@ package com.iluwatar.caching;
*
*/
public enum CachingPolicy {
THROUGH("through"), AROUND("around"), BEHIND("behind");
THROUGH("through"), AROUND("around"), BEHIND("behind"), ASIDE("aside");
private String policy;

View File

@ -60,4 +60,9 @@ public class CachingTest {
public void testReadThroughAndWriteBehindStrategy() {
app.useReadThroughAndWriteBehindStrategy();
}
@Test
public void testCacheAsideStrategy() {
app.useCacheAsideStategy();
}
}