#502 Replaced usages of System.out with logger.

This commit is contained in:
daniel-bryla
2016-10-23 19:59:03 +02:00
parent 4ca205c03c
commit 0438811489
154 changed files with 1155 additions and 792 deletions

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
@ -59,6 +62,9 @@ package com.iluwatar.caching;
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Program entry point
*
@ -80,13 +86,13 @@ public class App {
* Read-through and write-through
*/
public void useReadAndWriteThroughStrategy() {
System.out.println("# CachingPolicy.THROUGH");
LOGGER.info("# CachingPolicy.THROUGH");
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
AppManager.save(userAccount1);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("001");
AppManager.find("001");
}
@ -95,21 +101,21 @@ public class App {
* Read-through and write-around
*/
public void useReadThroughAndWriteAroundStrategy() {
System.out.println("# CachingPolicy.AROUND");
LOGGER.info("# CachingPolicy.AROUND");
AppManager.initCachingPolicy(CachingPolicy.AROUND);
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
AppManager.save(userAccount2);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("002");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
userAccount2 = AppManager.find("002");
userAccount2.setUserName("Jane G.");
AppManager.save(userAccount2);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("002");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("002");
}
@ -117,7 +123,7 @@ public class App {
* Read-through and write-behind
*/
public void useReadThroughAndWriteBehindStrategy() {
System.out.println("# CachingPolicy.BEHIND");
LOGGER.info("# CachingPolicy.BEHIND");
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
@ -127,13 +133,13 @@ public class App {
AppManager.save(userAccount3);
AppManager.save(userAccount4);
AppManager.save(userAccount5);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("003");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
AppManager.save(userAccount6);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("004");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
}
}

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
@ -31,6 +34,8 @@ import java.util.List;
*/
public class CacheStore {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheStore.class);
static LruCache cache;
private CacheStore() {
@ -52,10 +57,10 @@ public class CacheStore {
*/
public static UserAccount readThrough(String userId) {
if (cache.contains(userId)) {
System.out.println("# Cache Hit!");
LOGGER.info("# Cache Hit!");
return cache.get(userId);
}
System.out.println("# Cache Miss!");
LOGGER.info("# Cache Miss!");
UserAccount userAccount = DbManager.readFromDb(userId);
cache.set(userId, userAccount);
return userAccount;
@ -91,13 +96,13 @@ public class CacheStore {
*/
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
if (cache.contains(userId)) {
System.out.println("# Cache Hit!");
LOGGER.info("# Cache Hit!");
return cache.get(userId);
}
System.out.println("# Cache Miss!");
LOGGER.info("# Cache Miss!");
UserAccount userAccount = DbManager.readFromDb(userId);
if (cache.isFull()) {
System.out.println("# Cache is FULL! Writing LRU data to DB...");
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
UserAccount toBeWrittenToDb = cache.getLruData();
DbManager.upsertDb(toBeWrittenToDb);
}
@ -110,7 +115,7 @@ public class CacheStore {
*/
public static void writeBehind(UserAccount userAccount) {
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
System.out.println("# Cache is FULL! Writing LRU data to DB...");
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
UserAccount toBeWrittenToDb = cache.getLruData();
DbManager.upsertDb(toBeWrittenToDb);
}
@ -130,7 +135,7 @@ public class CacheStore {
* Writes remaining content in the cache into the DB.
*/
public static void flushCache() {
System.out.println("# flushCache...");
LOGGER.info("# flushCache...");
if (null == cache) {
return;
}

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -38,6 +41,8 @@ import java.util.Map;
*/
public class LruCache {
private static final Logger LOGGER = LoggerFactory.getLogger(LruCache.class);
class Node {
String userId;
UserAccount userAccount;
@ -117,7 +122,7 @@ public class LruCache {
} else {
Node newNode = new Node(userId, userAccount);
if (cache.size() >= capacity) {
System.out.println("# Cache is FULL! Removing " + end.userId + " from cache...");
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
cache.remove(end.userId); // remove LRU data from cache.
remove(end);
setHead(newNode);
@ -136,7 +141,7 @@ public class LruCache {
* Invalidate cache for user
*/
public void invalidate(String userId) {
System.out.println("# " + userId + " has been updated! Removing older version from cache...");
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
Node toBeRemoved = cache.get(userId);
remove(toBeRemoved);
cache.remove(userId);