Merge branch 'master' of https://github.com/iluwatar/java-design-patterns
* 'master' of https://github.com/iluwatar/java-design-patterns: (27 commits) Remove use of coveralls-maven-plugin (sonarqube.com covers this) Add SonarQube.com badge Fix environment variable Add Travis instructions for SonarQube.com analysis Adds more criticism to Singleton pattern. Event Based Asynchronous pattern: Add missing license header and puml diagram Changed config to non-interactive Moved config into a separate dir Unused import removed. End process logic clause has been corrected. Caching pattern: Documentation and diagram Fixes #437. Adds criticism to Singleton pattern. Alter JUnit tests to run in lesser time. Updated version snapshot to 1.14.0 Changes based on review feedback. Closes #436. Adds criticism to service locator pattern. Caching pattern: Implementation of Cache-Aside pattern Caching pattern: Style fix for null check Caching pattern: Refactor LRU cache to avoid NPE and unnecessary cache lookup Caching pattern: Refactor shutdown hook to use method reference ...
This commit is contained in:
@ -29,22 +29,23 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
|
||||
* the resources immediately after their use. The resources retain their identity, are kept in some
|
||||
* fast-access storage, and are re-used to avoid having to acquire them again. There are three main
|
||||
* caching strategies/techniques in this pattern; each with their own pros and cons. They are:
|
||||
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
|
||||
* caching strategies/techniques in this pattern; each with their own pros and cons. They are;
|
||||
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
|
||||
* <code>write-around</code> which writes data immediately into the DB instead of the cache, and
|
||||
* <code>write-around</code> which writes data immediately into the DB instead of the cache,
|
||||
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
|
||||
* written into the DB when the cache is full. The <code>read-through</code> strategy is also
|
||||
* included in the mentioned three strategies -- returns data from the cache to the caller <b>if</b>
|
||||
* it exists <b>else</b> queries from DB and stores it into the cache for future use. These
|
||||
* strategies determine when the data in the cache should be written back to the backing store (i.e.
|
||||
* Database) and help keep both data sources synchronized/up-to-date. This pattern can improve
|
||||
* performance and also helps to maintain consistency between data held in the cache and the data in
|
||||
* the underlying data store.
|
||||
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
|
||||
* responsibility of keeping the data synchronized in both data sources to the application itself.
|
||||
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
|
||||
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
|
||||
* stores it into the cache for future use. These strategies determine when the data in the cache
|
||||
* should be written back to the backing store (i.e. Database) and help keep both data sources
|
||||
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
|
||||
* consistency between data held in the cache and the data in the underlying data store.
|
||||
* <p>
|
||||
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
|
||||
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
|
||||
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
|
||||
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
|
||||
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
||||
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
|
||||
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
||||
@ -80,6 +81,7 @@ public class App {
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
app.useCacheAsideStategy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,4 +144,26 @@ public class App {
|
||||
AppManager.find("004");
|
||||
LOGGER.info(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());
|
||||
}
|
||||
}
|
||||
|
@ -64,12 +64,7 @@ public final class AppManager {
|
||||
public static void initCachingPolicy(CachingPolicy policy) {
|
||||
cachingPolicy = policy;
|
||||
if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CacheStore.flushCache();
|
||||
}
|
||||
}));
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(CacheStore::flushCache));
|
||||
}
|
||||
CacheStore.clearCache();
|
||||
}
|
||||
@ -86,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;
|
||||
}
|
||||
@ -100,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;
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public class CacheStore {
|
||||
* Init cache capacity
|
||||
*/
|
||||
public static void initCapacity(int capacity) {
|
||||
if (null == cache) {
|
||||
if (cache == null) {
|
||||
cache = new LruCache(capacity);
|
||||
} else {
|
||||
cache.setCapacity(capacity);
|
||||
@ -126,7 +126,7 @@ public class CacheStore {
|
||||
* Clears cache
|
||||
*/
|
||||
public static void clearCache() {
|
||||
if (null != cache) {
|
||||
if (cache != null) {
|
||||
cache.clear();
|
||||
}
|
||||
}
|
||||
@ -158,4 +158,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);
|
||||
}
|
||||
}
|
||||
|
@ -24,11 +24,11 @@ package com.iluwatar.caching;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enum class containing the three caching strategies implemented in the pattern.
|
||||
* Enum class containing the four caching strategies implemented in the pattern.
|
||||
*
|
||||
*/
|
||||
public enum CachingPolicy {
|
||||
THROUGH("through"), AROUND("around"), BEHIND("behind");
|
||||
THROUGH("through"), AROUND("around"), BEHIND("behind"), ASIDE("aside");
|
||||
|
||||
private String policy;
|
||||
|
||||
|
@ -82,7 +82,7 @@ public final class DbManager {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (null == db) {
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
@ -106,7 +106,7 @@ public final class DbManager {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (null == db) {
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
@ -126,7 +126,7 @@ public final class DbManager {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (null == db) {
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
@ -148,7 +148,7 @@ public final class DbManager {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (null == db) {
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
|
@ -78,7 +78,6 @@ public class LruCache {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Remove node from linked list.
|
||||
*/
|
||||
public void remove(Node node) {
|
||||
@ -95,7 +94,6 @@ public class LruCache {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Move node to the front of the list.
|
||||
*/
|
||||
public void setHead(Node node) {
|
||||
@ -141,10 +139,11 @@ public class LruCache {
|
||||
* Invalidate cache for user
|
||||
*/
|
||||
public void invalidate(String userId) {
|
||||
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
||||
Node toBeRemoved = cache.get(userId);
|
||||
remove(toBeRemoved);
|
||||
cache.remove(userId);
|
||||
Node toBeRemoved = cache.remove(userId);
|
||||
if (toBeRemoved != null) {
|
||||
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
||||
remove(toBeRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
@ -165,7 +164,6 @@ public class LruCache {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns cache data in list form.
|
||||
*/
|
||||
public List<UserAccount> getCacheDataInListForm() {
|
||||
|
@ -23,9 +23,7 @@
|
||||
package com.iluwatar.caching;
|
||||
|
||||
/**
|
||||
*
|
||||
* Entity class (stored in cache and DB) used in the application.
|
||||
*
|
||||
*/
|
||||
public class UserAccount {
|
||||
private String userId;
|
||||
|
Reference in New Issue
Block a user