Skip to content

Commit 399ad53

Browse files
committed
Changed database implementation. Removed static objects.
1 parent bbdff14 commit 399ad53

File tree

9 files changed

+339
-333
lines changed

9 files changed

+339
-333
lines changed

caching/src/main/java/com/iluwatar/caching/App.java

Lines changed: 130 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
package com.iluwatar.caching;
2525

26+
import com.iluwatar.caching.database.DbManager;
27+
import com.iluwatar.caching.database.DbManagerFactory;
2628
import lombok.extern.slf4j.Slf4j;
2729

2830
/**
@@ -55,111 +57,138 @@
5557
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i>
5658
* </p>
5759
*
60+
* <p>
61+
* To run the application with MongoDb, just start it with parameter --mongo
62+
* Example: java -jar app.jar --mongo
63+
* </p>
64+
*
5865
* @see CacheStore
5966
* @see LruCache
6067
* @see CachingPolicy
6168
*/
6269
@Slf4j
6370
public class App {
64-
65-
/**
66-
* Program entry point.
67-
*
68-
* @param args command line args
69-
*/
70-
public static void main(String[] args) {
71-
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
72-
// and the App class to avoid Maven compilation errors. Set flag to
73-
// true to run the tests with MongoDB (provided that MongoDB is
74-
// installed and socket connection is open).
75-
AppManager.initCacheCapacity(3);
76-
var app = new App();
77-
app.useReadAndWriteThroughStrategy();
78-
app.useReadThroughAndWriteAroundStrategy();
79-
app.useReadThroughAndWriteBehindStrategy();
80-
app.useCacheAsideStategy();
81-
}
82-
83-
/**
84-
* Read-through and write-through.
85-
*/
86-
public void useReadAndWriteThroughStrategy() {
87-
LOGGER.info("# CachingPolicy.THROUGH");
88-
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
89-
90-
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
91-
92-
AppManager.save(userAccount1);
93-
LOGGER.info(AppManager.printCacheContent());
94-
AppManager.find("001");
95-
AppManager.find("001");
96-
}
97-
98-
/**
99-
* Read-through and write-around.
100-
*/
101-
public void useReadThroughAndWriteAroundStrategy() {
102-
LOGGER.info("# CachingPolicy.AROUND");
103-
AppManager.initCachingPolicy(CachingPolicy.AROUND);
104-
105-
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
106-
107-
AppManager.save(userAccount2);
108-
LOGGER.info(AppManager.printCacheContent());
109-
AppManager.find("002");
110-
LOGGER.info(AppManager.printCacheContent());
111-
userAccount2 = AppManager.find("002");
112-
userAccount2.setUserName("Jane G.");
113-
AppManager.save(userAccount2);
114-
LOGGER.info(AppManager.printCacheContent());
115-
AppManager.find("002");
116-
LOGGER.info(AppManager.printCacheContent());
117-
AppManager.find("002");
118-
}
119-
120-
/**
121-
* Read-through and write-behind.
122-
*/
123-
public void useReadThroughAndWriteBehindStrategy() {
124-
LOGGER.info("# CachingPolicy.BEHIND");
125-
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
126-
127-
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
128-
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
129-
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
130-
131-
AppManager.save(userAccount3);
132-
AppManager.save(userAccount4);
133-
AppManager.save(userAccount5);
134-
LOGGER.info(AppManager.printCacheContent());
135-
AppManager.find("003");
136-
LOGGER.info(AppManager.printCacheContent());
137-
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
138-
AppManager.save(userAccount6);
139-
LOGGER.info(AppManager.printCacheContent());
140-
AppManager.find("004");
141-
LOGGER.info(AppManager.printCacheContent());
142-
}
143-
144-
/**
145-
* Cache-Aside.
146-
*/
147-
public void useCacheAsideStategy() {
148-
LOGGER.info("# CachingPolicy.ASIDE");
149-
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
150-
LOGGER.info(AppManager.printCacheContent());
151-
152-
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
153-
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
154-
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
155-
AppManager.save(userAccount3);
156-
AppManager.save(userAccount4);
157-
AppManager.save(userAccount5);
158-
159-
LOGGER.info(AppManager.printCacheContent());
160-
AppManager.find("003");
161-
LOGGER.info(AppManager.printCacheContent());
162-
AppManager.find("004");
163-
LOGGER.info(AppManager.printCacheContent());
164-
}
71+
private static final String USE_MONGO_DB = "--mongo";
72+
private DbManager dbManager;
73+
private AppManager appManager;
74+
75+
public App(boolean isMongo) {
76+
dbManager = DbManagerFactory.initDb(isMongo);
77+
appManager = new AppManager(dbManager);
78+
appManager.initDb();
79+
}
80+
81+
/**
82+
* Program entry point.
83+
*
84+
* @param args command line args
85+
*/
86+
public static void main(String[] args) {
87+
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
88+
// and the App class to avoid Maven compilation errors. Set flag to
89+
// true to run the tests with MongoDB (provided that MongoDB is
90+
// installed and socket connection is open).
91+
App app = new App(isDbMongo(args));
92+
app.useReadAndWriteThroughStrategy();
93+
app.useReadThroughAndWriteAroundStrategy();
94+
app.useReadThroughAndWriteBehindStrategy();
95+
app.useCacheAsideStategy();
96+
}
97+
98+
/**
99+
* Check the input parameters. if
100+
* @param args input params
101+
* @return true if there is "--mongo" parameter in arguments
102+
*/
103+
private static boolean isDbMongo(String[] args) {
104+
for (String arg : args) {
105+
if (arg.equals(USE_MONGO_DB)) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
112+
/**
113+
* Read-through and write-through.
114+
*/
115+
public void useReadAndWriteThroughStrategy() {
116+
LOGGER.info("# CachingPolicy.THROUGH");
117+
appManager.initCachingPolicy(CachingPolicy.THROUGH);
118+
119+
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
120+
121+
appManager.save(userAccount1);
122+
LOGGER.info(appManager.printCacheContent());
123+
appManager.find("001");
124+
appManager.find("001");
125+
}
126+
127+
/**
128+
* Read-through and write-around.
129+
*/
130+
public void useReadThroughAndWriteAroundStrategy() {
131+
LOGGER.info("# CachingPolicy.AROUND");
132+
appManager.initCachingPolicy(CachingPolicy.AROUND);
133+
134+
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
135+
136+
appManager.save(userAccount2);
137+
LOGGER.info(appManager.printCacheContent());
138+
appManager.find("002");
139+
LOGGER.info(appManager.printCacheContent());
140+
userAccount2 = appManager.find("002");
141+
userAccount2.setUserName("Jane G.");
142+
appManager.save(userAccount2);
143+
LOGGER.info(appManager.printCacheContent());
144+
appManager.find("002");
145+
LOGGER.info(appManager.printCacheContent());
146+
appManager.find("002");
147+
}
148+
149+
/**
150+
* Read-through and write-behind.
151+
*/
152+
public void useReadThroughAndWriteBehindStrategy() {
153+
LOGGER.info("# CachingPolicy.BEHIND");
154+
appManager.initCachingPolicy(CachingPolicy.BEHIND);
155+
156+
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
157+
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
158+
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
159+
160+
appManager.save(userAccount3);
161+
appManager.save(userAccount4);
162+
appManager.save(userAccount5);
163+
LOGGER.info(appManager.printCacheContent());
164+
appManager.find("003");
165+
LOGGER.info(appManager.printCacheContent());
166+
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
167+
appManager.save(userAccount6);
168+
LOGGER.info(appManager.printCacheContent());
169+
appManager.find("004");
170+
LOGGER.info(appManager.printCacheContent());
171+
}
172+
173+
/**
174+
* Cache-Aside.
175+
*/
176+
public void useCacheAsideStategy() {
177+
LOGGER.info("# CachingPolicy.ASIDE");
178+
appManager.initCachingPolicy(CachingPolicy.ASIDE);
179+
LOGGER.info(appManager.printCacheContent());
180+
181+
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
182+
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
183+
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
184+
appManager.save(userAccount3);
185+
appManager.save(userAccount4);
186+
appManager.save(userAccount5);
187+
188+
LOGGER.info(appManager.printCacheContent());
189+
appManager.find("003");
190+
LOGGER.info(appManager.printCacheContent());
191+
appManager.find("004");
192+
LOGGER.info(appManager.printCacheContent());
193+
}
165194
}

caching/src/main/java/com/iluwatar/caching/AppManager.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
package com.iluwatar.caching;
2525

26-
import java.text.ParseException;
2726
import java.util.Optional;
27+
28+
import com.iluwatar.caching.database.DbManager;
2829
import lombok.extern.slf4j.Slf4j;
2930

3031
/**
@@ -35,53 +36,45 @@
3536
* CacheStore class.
3637
*/
3738
@Slf4j
38-
public final class AppManager {
39+
public class AppManager {
3940

4041
private static CachingPolicy cachingPolicy;
42+
private final DbManager dbManager;
43+
private final CacheStore cacheStore;
4144

42-
private AppManager() {
45+
public AppManager(DbManager dbManager) {
46+
this.dbManager = dbManager;
47+
this.cacheStore = new CacheStore(dbManager);
4348
}
4449

4550
/**
4651
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
4752
* data storage or a simple Java data structure to (temporarily) store the data/objects during
4853
* runtime.
4954
*/
50-
public static void initDb(boolean useMongoDb) {
51-
if (useMongoDb) {
52-
try {
53-
DbManager.connect();
54-
} catch (ParseException e) {
55-
LOGGER.error("Error connecting to MongoDB", e);
56-
}
57-
} else {
58-
DbManager.createVirtualDb();
59-
}
55+
public void initDb() {
56+
dbManager.connect();
6057
}
6158

6259
/**
6360
* Initialize caching policy.
6461
*/
65-
public static void initCachingPolicy(CachingPolicy policy) {
62+
public void initCachingPolicy(CachingPolicy policy) {
6663
cachingPolicy = policy;
6764
if (cachingPolicy == CachingPolicy.BEHIND) {
68-
Runtime.getRuntime().addShutdownHook(new Thread(CacheStore::flushCache));
65+
Runtime.getRuntime().addShutdownHook(new Thread(cacheStore::flushCache));
6966
}
70-
CacheStore.clearCache();
71-
}
72-
73-
public static void initCacheCapacity(int capacity) {
74-
CacheStore.initCapacity(capacity);
67+
cacheStore.clearCache();
7568
}
7669

7770
/**
7871
* Find user account.
7972
*/
80-
public static UserAccount find(String userId) {
73+
public UserAccount find(String userId) {
8174
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
82-
return CacheStore.readThrough(userId);
75+
return cacheStore.readThrough(userId);
8376
} else if (cachingPolicy == CachingPolicy.BEHIND) {
84-
return CacheStore.readThroughWithWriteBackPolicy(userId);
77+
return cacheStore.readThroughWithWriteBackPolicy(userId);
8578
} else if (cachingPolicy == CachingPolicy.ASIDE) {
8679
return findAside(userId);
8780
}
@@ -91,38 +84,38 @@ public static UserAccount find(String userId) {
9184
/**
9285
* Save user account.
9386
*/
94-
public static void save(UserAccount userAccount) {
87+
public void save(UserAccount userAccount) {
9588
if (cachingPolicy == CachingPolicy.THROUGH) {
96-
CacheStore.writeThrough(userAccount);
89+
cacheStore.writeThrough(userAccount);
9790
} else if (cachingPolicy == CachingPolicy.AROUND) {
98-
CacheStore.writeAround(userAccount);
91+
cacheStore.writeAround(userAccount);
9992
} else if (cachingPolicy == CachingPolicy.BEHIND) {
100-
CacheStore.writeBehind(userAccount);
93+
cacheStore.writeBehind(userAccount);
10194
} else if (cachingPolicy == CachingPolicy.ASIDE) {
10295
saveAside(userAccount);
10396
}
10497
}
10598

106-
public static String printCacheContent() {
107-
return CacheStore.print();
99+
public String printCacheContent() {
100+
return cacheStore.print();
108101
}
109102

110103
/**
111104
* Cache-Aside save user account helper.
112105
*/
113-
private static void saveAside(UserAccount userAccount) {
114-
DbManager.updateDb(userAccount);
115-
CacheStore.invalidate(userAccount.getUserId());
106+
private void saveAside(UserAccount userAccount) {
107+
dbManager.updateDb(userAccount);
108+
cacheStore.invalidate(userAccount.getUserId());
116109
}
117110

118111
/**
119112
* Cache-Aside find user account helper.
120113
*/
121-
private static UserAccount findAside(String userId) {
122-
return Optional.ofNullable(CacheStore.get(userId))
114+
private UserAccount findAside(String userId) {
115+
return Optional.ofNullable(cacheStore.get(userId))
123116
.or(() -> {
124-
Optional<UserAccount> userAccount = Optional.ofNullable(DbManager.readFromDb(userId));
125-
userAccount.ifPresent(account -> CacheStore.set(userId, account));
117+
Optional<UserAccount> userAccount = Optional.ofNullable(dbManager.readFromDb(userId));
118+
userAccount.ifPresent(account -> cacheStore.set(userId, account));
126119
return userAccount;
127120
})
128121
.orElse(null);

0 commit comments

Comments
 (0)