Caching pattern: Refactor LRU cache to avoid NPE and unnecessary cache lookup

This commit is contained in:
Christoffer Hamberg 2016-09-22 09:38:23 +02:00
parent 6ed842e58b
commit 865f788612

View File

@ -136,10 +136,11 @@ public class LruCache {
* Invalidate cache for user * Invalidate cache for user
*/ */
public void invalidate(String userId) { public void invalidate(String userId) {
System.out.println("# " + userId + " has been updated! Removing older version from cache..."); Node toBeRemoved = cache.remove(userId);
Node toBeRemoved = cache.get(userId); if (toBeRemoved != null) {
remove(toBeRemoved); System.out.println("# " + userId + " has been updated! Removing older version from cache...");
cache.remove(userId); remove(toBeRemoved);
}
} }
public boolean isFull() { public boolean isFull() {