Repository: iluwatar/java-design-patterns
Issue: Cache Aside pattern
Labels: epic: pattern

https://msdn.microsoft.com/en-us/library/dn589799.aspx

This pattern could be incorporated into https://github.com/iluwatar/java-design-patterns/tree/master/caching example.


Issue comments:
Thanks for an excellent project, I thought I should contribute something back and this one seemed like a low-hanging fruit.

Would it make sense to simply add two helper methods in AppManager, e.g. `saveAside` and `findAside`

``` java
private static void saveAside(UserAccount userAccount) {
    DbManager.updateDb(userAccount);
    CacheStore.invalidate(userAccount.getUserId());
}
```

and then either delegate `CacheStore.(get,set,invalidate)` to `cache`

``` java
public static void invalidate(String userId) {
    cache.invalidate(userId);
}
```

or simply expose `cache` with a `CacheStore.getCache()` getter?

And then add a new case:

``` java
public static void save(UserAccount userAccount) {
    if (cachingPolicy == CachingPolicy.THROUGH) {
      CacheStore.writeThrough(userAccount);
    } else if (cachingPolicy == CachingPolicy.AROUND) {
      CacheStore.writeAround(userAccount);
    } else if (cachingPolicy == CachingPolicy.BEHIND) {
      CacheStore.writeBehind(userAccount);
    } else if (cachingPolicy == CachingPolicy.ASIDE) {
      saveAside(userAccount);
}
```

Would that make sense, @iluwatar? 

@christofferh the plan looks reasonable. Just create a pull request and we'll review.


Pull request review comments:
Is there any reason for not using logging framework like SLF4J or LOG4J Or Common logging?

@chinnachalam you are right, using a logging framework would give some advantages such as ability to redirect output and filter output based on logging levels. But I think we should handle this issue on a more general level since there are multiple patterns not using a logging framework at the moment. Would you raise an issue about this matter, please?


Patch that resolved the issue:
diff --git a/caching/README.md b/caching/README.md
index 6432ffcea26e..b48d060f0012 100644
--- a/caching/README.md
+++ b/caching/README.md
@@ -27,3 +27,4 @@ Use the Caching pattern(s) when
 
 * [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
 * [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
+* [Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)
diff --git a/caching/etc/caching.ucls b/caching/etc/caching.ucls
index 815a62ec99ce..a058277ea2fd 100644
--- a/caching/etc/caching.ucls
+++ b/caching/etc/caching.ucls
@@ -1,106 +1,90 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true" 
-  realizations="true" associations="true" dependencies="false" nesting-relationships="true">  
-  <class id="1" language="java" name="main.java.com.wssia.caching.App" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/App.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="249" y="150"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
+<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
+  generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
+  router="FAN">
+  <class id="1" language="java" name="com.iluwatar.caching.CacheStore" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="322" y="444"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </class>
+  <class id="2" language="java" name="com.iluwatar.caching.LruCache" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/LruCache.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="700" y="446"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </class>
+  <enumeration id="3" language="java" name="com.iluwatar.caching.CachingPolicy" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="943" y="122"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </enumeration>
+  <class id="4" language="java" name="com.iluwatar.caching.DbManager" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/DbManager.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="1011" y="426"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </class>
+  <class id="5" language="java" name="com.iluwatar.caching.App" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="304" y="95"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </class>
+  <class id="6" language="java" name="com.iluwatar.caching.AppManager" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="604" y="122"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
+  </class>
+  <class id="7" language="java" name="com.iluwatar.caching.UserAccount" project="caching"
+    file="/caching/src/main/java/com/iluwatar/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
+    <position height="-1" width="-1" x="1161" y="139"/>
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+      sort-features="false" accessors="true" visibility="true">
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>
+      <operations public="true" package="true" protected="true" private="true" static="true"/>
+    </display>
   </class>  
-  <class id="2" language="java" name="main.java.com.wssia.caching.AppManager" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="502" y="163"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <class id="3" language="java" name="main.java.com.wssia.caching.CacheStore" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="537" y="436"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <enumeration id="4" language="java" name="main.java.com.wssia.caching.CachingPolicy" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="789" y="162"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </enumeration>  
-  <class id="5" language="java" name="main.java.com.wssia.caching.DBManager" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/DBManager.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="1137" y="134"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <class id="6" language="java" name="main.java.com.wssia.caching.LRUCache" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/LRUCache.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="884" y="435"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns" 
-    file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="1140" y="405"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <class id="8" language="java" name="test.java.com.wssia.caching.AppTest" project="CachingPatterns" 
-    file="/CachingPatterns/src/test/java/com/wssia/caching/AppTest.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="-1" width="-1" x="251" y="374"/>    
-    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-      sort-features="false" accessors="true" visibility="true">      
-      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
-      <operations public="true" package="true" protected="true" private="true" static="true"/>    
-    </display>  
-  </class>  
-  <association id="9">    
-    <end type="SOURCE" refId="2" navigable="false">      
-      <attribute id="10" name="cachingPolicy"/>      
-      <multiplicity id="11" minimum="0" maximum="1"/>    
-    </end>    
-    <end type="TARGET" refId="4" navigable="true"/>    
-    <display labels="true" multiplicity="true"/>  
-  </association>  
-  <association id="12">    
-    <end type="SOURCE" refId="8" navigable="false">      
-      <attribute id="13" name="app"/>      
-      <multiplicity id="14" minimum="0" maximum="1"/>    
-    </end>    
-    <end type="TARGET" refId="1" navigable="true"/>    
-    <display labels="true" multiplicity="true"/>  
-  </association>  
-  <association id="15">    
-    <end type="SOURCE" refId="3" navigable="false">      
-      <attribute id="16" name="cache"/>      
-      <multiplicity id="17" minimum="0" maximum="1"/>    
-    </end>    
-    <end type="TARGET" refId="6" navigable="true"/>    
-    <display labels="true" multiplicity="true"/>  
-  </association>  
-  <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
-    sort-features="false" accessors="true" visibility="true">    
-    <attributes public="true" package="true" protected="true" private="true" static="true"/>    
-    <operations public="true" package="true" protected="true" private="true" static="true"/>  
-  </classifier-display>  
+  <association id="8">
+    <end type="SOURCE" refId="1" navigable="false">
+      <attribute id="9" name="cache"/>
+      <multiplicity id="10" minimum="0" maximum="1"/>
+    </end>
+    <end type="TARGET" refId="2" navigable="true"/>
+    <display labels="true" multiplicity="true"/>
+  </association>
+  <association id="11">
+    <end type="SOURCE" refId="6" navigable="false">
+      <attribute id="12" name="cachingPolicy"/>
+      <multiplicity id="13" minimum="0" maximum="1"/>
+    </end>
+    <end type="TARGET" refId="3" navigable="true"/>
+    <display labels="true" multiplicity="true"/>
+  </association>
+  <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
+    sort-features="false" accessors="true" visibility="true">
+    <attributes public="true" package="true" protected="true" private="true" static="true"/>
+    <operations public="true" package="true" protected="true" private="true" static="true"/>
+  </classifier-display>
   <association-display labels="true" multiplicity="true"/>
-</class-diagram>
\ No newline at end of file
+</class-diagram>
diff --git a/caching/src/main/java/com/iluwatar/caching/App.java b/caching/src/main/java/com/iluwatar/caching/App.java
index 8e5a84085ff9..1b1b5e1cac91 100644
--- a/caching/src/main/java/com/iluwatar/caching/App.java
+++ b/caching/src/main/java/com/iluwatar/caching/App.java
@@ -26,22 +26,23 @@
  *
  * 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
@@ -74,6 +75,7 @@ public static void main(String[] args) {
     app.useReadAndWriteThroughStrategy();
     app.useReadThroughAndWriteAroundStrategy();
     app.useReadThroughAndWriteBehindStrategy();
+    app.useCacheAsideStategy();
   }
 
   /**
@@ -136,4 +138,26 @@ public void useReadThroughAndWriteBehindStrategy() {
     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());
+  }
 }
diff --git a/caching/src/main/java/com/iluwatar/caching/AppManager.java b/caching/src/main/java/com/iluwatar/caching/AppManager.java
index 2967c759f7b7..2d6a424a35b4 100644
--- a/caching/src/main/java/com/iluwatar/caching/AppManager.java
+++ b/caching/src/main/java/com/iluwatar/caching/AppManager.java
@@ -64,12 +64,7 @@ public static void initDb(boolean useMongoDb) {
   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 static UserAccount find(String userId) {
       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 static void save(UserAccount userAccount) {
       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;
+  }
 }
diff --git a/caching/src/main/java/com/iluwatar/caching/CacheStore.java b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
index 5903f82193e9..c2b95a1bdb55 100644
--- a/caching/src/main/java/com/iluwatar/caching/CacheStore.java
+++ b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
@@ -40,7 +40,7 @@ private CacheStore() {
    * Init cache capacity
    */
   public static void initCapacity(int capacity) {
-    if (null == cache) {
+    if (cache == null) {
       cache = new LruCache(capacity);
     } else {
       cache.setCapacity(capacity);
@@ -121,7 +121,7 @@ public static void writeBehind(UserAccount userAccount) {
    * Clears cache
    */
   public static void clearCache() {
-    if (null != cache) {
+    if (cache != null) {
       cache.clear();
     }
   }
@@ -153,4 +153,25 @@ public static String print() {
     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);
+  }
 }
diff --git a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java
index 490113baa3d9..5ad32f6993a5 100644
--- a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java
+++ b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java
@@ -24,11 +24,11 @@
 
 /**
  *
- * 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;
 
diff --git a/caching/src/main/java/com/iluwatar/caching/DbManager.java b/caching/src/main/java/com/iluwatar/caching/DbManager.java
index c12461d0c690..455302106b57 100644
--- a/caching/src/main/java/com/iluwatar/caching/DbManager.java
+++ b/caching/src/main/java/com/iluwatar/caching/DbManager.java
@@ -82,7 +82,7 @@ public static UserAccount readFromDb(String userId) {
       }
       return null;
     }
-    if (null == db) {
+    if (db == null) {
       try {
         connect();
       } catch (ParseException e) {
@@ -106,7 +106,7 @@ public static void writeToDb(UserAccount userAccount) {
       virtualDB.put(userAccount.getUserId(), userAccount);
       return;
     }
-    if (null == db) {
+    if (db == null) {
       try {
         connect();
       } catch (ParseException e) {
@@ -126,7 +126,7 @@ public static void updateDb(UserAccount userAccount) {
       virtualDB.put(userAccount.getUserId(), userAccount);
       return;
     }
-    if (null == db) {
+    if (db == null) {
       try {
         connect();
       } catch (ParseException e) {
@@ -148,7 +148,7 @@ public static void upsertDb(UserAccount userAccount) {
       virtualDB.put(userAccount.getUserId(), userAccount);
       return;
     }
-    if (null == db) {
+    if (db == null) {
       try {
         connect();
       } catch (ParseException e) {
diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java
index 5c5549afd9d7..00e27ccf1fc1 100644
--- a/caching/src/main/java/com/iluwatar/caching/LruCache.java
+++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java
@@ -73,7 +73,6 @@ public UserAccount get(String userId) {
   }
 
   /**
-   *
    * Remove node from linked list.
    */
   public void remove(Node node) {
@@ -90,7 +89,6 @@ public void remove(Node node) {
   }
 
   /**
-   *
    * Move node to the front of the list.
    */
   public void setHead(Node node) {
@@ -136,10 +134,11 @@ public boolean contains(String userId) {
    * Invalidate cache for user
    */
   public void invalidate(String userId) {
-    System.out.println("# " + userId + " has been updated! Removing older version from cache...");
-    Node toBeRemoved = cache.get(userId);
-    remove(toBeRemoved);
-    cache.remove(userId);
+    Node toBeRemoved = cache.remove(userId);
+    if (toBeRemoved != null) {
+      System.out.println("# " + userId + " has been updated! Removing older version from cache...");
+      remove(toBeRemoved);
+    }
   }
 
   public boolean isFull() {
@@ -160,7 +159,6 @@ public void clear() {
   }
 
   /**
-   *
    * Returns cache data in list form.
    */
   public List<UserAccount> getCacheDataInListForm() {
diff --git a/caching/src/main/java/com/iluwatar/caching/UserAccount.java b/caching/src/main/java/com/iluwatar/caching/UserAccount.java
index e2444ad72769..657f12fa33f1 100644
--- a/caching/src/main/java/com/iluwatar/caching/UserAccount.java
+++ b/caching/src/main/java/com/iluwatar/caching/UserAccount.java
@@ -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;


Test patch:
diff --git a/caching/src/test/java/com/iluwatar/caching/CachingTest.java b/caching/src/test/java/com/iluwatar/caching/CachingTest.java
index 19262a3b6e35..9d2299902a8c 100644
--- a/caching/src/test/java/com/iluwatar/caching/CachingTest.java
+++ b/caching/src/test/java/com/iluwatar/caching/CachingTest.java
@@ -60,4 +60,9 @@ public void testReadThroughAndWriteAroundStrategy() {
   public void testReadThroughAndWriteBehindStrategy() {
     app.useReadThroughAndWriteBehindStrategy();
   }
+
+  @Test
+  public void testCacheAsideStrategy() {
+    app.useCacheAsideStategy();
+  }
 }