This commit is contained in:
Victor Zalevskii 2021-08-17 16:53:44 +03:00
parent 399ad53b09
commit a437f5b2eb
3 changed files with 12 additions and 7 deletions

View File

@ -69,11 +69,10 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class App {
private static final String USE_MONGO_DB = "--mongo";
private DbManager dbManager;
private AppManager appManager;
public App(boolean isMongo) {
dbManager = DbManagerFactory.initDb(isMongo);
DbManager dbManager = DbManagerFactory.initDb(isMongo);
appManager = new AppManager(dbManager);
appManager.initDb();
}
@ -90,9 +89,13 @@ public class App {
// installed and socket connection is open).
App app = new App(isDbMongo(args));
app.useReadAndWriteThroughStrategy();
System.out.println("=====================================================");
app.useReadThroughAndWriteAroundStrategy();
System.out.println("=====================================================");
app.useReadThroughAndWriteBehindStrategy();
System.out.println("=====================================================");
app.useCacheAsideStategy();
System.out.println("=====================================================");
}
/**

View File

@ -71,6 +71,7 @@ public class AppManager {
* Find user account.
*/
public UserAccount find(String userId) {
LOGGER.info("Trying to find {} in cache", userId);
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
return cacheStore.readThrough(userId);
} else if (cachingPolicy == CachingPolicy.BEHIND) {
@ -85,6 +86,7 @@ public class AppManager {
* Save user account.
*/
public void save(UserAccount userAccount) {
LOGGER.info("Save record!");
if (cachingPolicy == CachingPolicy.THROUGH) {
cacheStore.writeThrough(userAccount);
} else if (cachingPolicy == CachingPolicy.AROUND) {

View File

@ -61,10 +61,10 @@ public class CacheStore {
*/
public UserAccount readThrough(String userId) {
if (cache.contains(userId)) {
LOGGER.info("# Cache Hit!");
LOGGER.info("# Found in Cache!");
return cache.get(userId);
}
LOGGER.info("# Cache Miss!");
LOGGER.info("# Not found in cache! Go to DB!!");
UserAccount userAccount = dbManager.readFromDb(userId);
cache.set(userId, userAccount);
return userAccount;
@ -100,10 +100,10 @@ public class CacheStore {
*/
public UserAccount readThroughWithWriteBackPolicy(String userId) {
if (cache.contains(userId)) {
LOGGER.info("# Cache Hit!");
LOGGER.info("# Found in cache!");
return cache.get(userId);
}
LOGGER.info("# Cache Miss!");
LOGGER.info("# Not found in Cache!");
UserAccount userAccount = dbManager.readFromDb(userId);
if (cache.isFull()) {
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
@ -155,7 +155,7 @@ public class CacheStore {
.orElse(List.of())
.stream()
.map(userAccount -> userAccount.toString() + "\n")
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----\n"));
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----"));
}
/**