Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit399ad53

Browse files
committed
Changed database implementation. Removed static objects.
1 parentbbdff14 commit399ad53

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
packagecom.iluwatar.caching;
2525

26+
importcom.iluwatar.caching.database.DbManager;
27+
importcom.iluwatar.caching.database.DbManagerFactory;
2628
importlombok.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
publicclassApp {
64-
65-
/**
66-
* Program entry point.
67-
*
68-
* @param args command line args
69-
*/
70-
publicstaticvoidmain(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-
varapp =newApp();
77-
app.useReadAndWriteThroughStrategy();
78-
app.useReadThroughAndWriteAroundStrategy();
79-
app.useReadThroughAndWriteBehindStrategy();
80-
app.useCacheAsideStategy();
81-
}
82-
83-
/**
84-
* Read-through and write-through.
85-
*/
86-
publicvoiduseReadAndWriteThroughStrategy() {
87-
LOGGER.info("# CachingPolicy.THROUGH");
88-
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
89-
90-
varuserAccount1 =newUserAccount("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-
publicvoiduseReadThroughAndWriteAroundStrategy() {
102-
LOGGER.info("# CachingPolicy.AROUND");
103-
AppManager.initCachingPolicy(CachingPolicy.AROUND);
104-
105-
varuserAccount2 =newUserAccount("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-
publicvoiduseReadThroughAndWriteBehindStrategy() {
124-
LOGGER.info("# CachingPolicy.BEHIND");
125-
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
126-
127-
varuserAccount3 =newUserAccount("003","Adam","He likes food.");
128-
varuserAccount4 =newUserAccount("004","Rita","She hates cats.");
129-
varuserAccount5 =newUserAccount("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-
UserAccountuserAccount6 =newUserAccount("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-
publicvoiduseCacheAsideStategy() {
148-
LOGGER.info("# CachingPolicy.ASIDE");
149-
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
150-
LOGGER.info(AppManager.printCacheContent());
151-
152-
varuserAccount3 =newUserAccount("003","Adam","He likes food.");
153-
varuserAccount4 =newUserAccount("004","Rita","She hates cats.");
154-
varuserAccount5 =newUserAccount("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+
privatestaticfinalStringUSE_MONGO_DB ="--mongo";
72+
privateDbManagerdbManager;
73+
privateAppManagerappManager;
74+
75+
publicApp(booleanisMongo) {
76+
dbManager =DbManagerFactory.initDb(isMongo);
77+
appManager =newAppManager(dbManager);
78+
appManager.initDb();
79+
}
80+
81+
/**
82+
* Program entry point.
83+
*
84+
* @param args command line args
85+
*/
86+
publicstaticvoidmain(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+
Appapp =newApp(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+
privatestaticbooleanisDbMongo(String[]args) {
104+
for (Stringarg :args) {
105+
if (arg.equals(USE_MONGO_DB)) {
106+
returntrue;
107+
}
108+
}
109+
returnfalse;
110+
}
111+
112+
/**
113+
* Read-through and write-through.
114+
*/
115+
publicvoiduseReadAndWriteThroughStrategy() {
116+
LOGGER.info("# CachingPolicy.THROUGH");
117+
appManager.initCachingPolicy(CachingPolicy.THROUGH);
118+
119+
varuserAccount1 =newUserAccount("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+
publicvoiduseReadThroughAndWriteAroundStrategy() {
131+
LOGGER.info("# CachingPolicy.AROUND");
132+
appManager.initCachingPolicy(CachingPolicy.AROUND);
133+
134+
varuserAccount2 =newUserAccount("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+
publicvoiduseReadThroughAndWriteBehindStrategy() {
153+
LOGGER.info("# CachingPolicy.BEHIND");
154+
appManager.initCachingPolicy(CachingPolicy.BEHIND);
155+
156+
varuserAccount3 =newUserAccount("003","Adam","He likes food.");
157+
varuserAccount4 =newUserAccount("004","Rita","She hates cats.");
158+
varuserAccount5 =newUserAccount("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+
UserAccountuserAccount6 =newUserAccount("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+
publicvoiduseCacheAsideStategy() {
177+
LOGGER.info("# CachingPolicy.ASIDE");
178+
appManager.initCachingPolicy(CachingPolicy.ASIDE);
179+
LOGGER.info(appManager.printCacheContent());
180+
181+
varuserAccount3 =newUserAccount("003","Adam","He likes food.");
182+
varuserAccount4 =newUserAccount("004","Rita","She hates cats.");
183+
varuserAccount5 =newUserAccount("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
packagecom.iluwatar.caching;
2525

26-
importjava.text.ParseException;
2726
importjava.util.Optional;
27+
28+
importcom.iluwatar.caching.database.DbManager;
2829
importlombok.extern.slf4j.Slf4j;
2930

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

4041
privatestaticCachingPolicycachingPolicy;
42+
privatefinalDbManagerdbManager;
43+
privatefinalCacheStorecacheStore;
4144

42-
privateAppManager() {
45+
publicAppManager(DbManagerdbManager) {
46+
this.dbManager =dbManager;
47+
this.cacheStore =newCacheStore(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-
publicstaticvoidinitDb(booleanuseMongoDb) {
51-
if (useMongoDb) {
52-
try {
53-
DbManager.connect();
54-
}catch (ParseExceptione) {
55-
LOGGER.error("Error connecting to MongoDB",e);
56-
}
57-
}else {
58-
DbManager.createVirtualDb();
59-
}
55+
publicvoidinitDb() {
56+
dbManager.connect();
6057
}
6158

6259
/**
6360
* Initialize caching policy.
6461
*/
65-
publicstaticvoidinitCachingPolicy(CachingPolicypolicy) {
62+
publicvoidinitCachingPolicy(CachingPolicypolicy) {
6663
cachingPolicy =policy;
6764
if (cachingPolicy ==CachingPolicy.BEHIND) {
68-
Runtime.getRuntime().addShutdownHook(newThread(CacheStore::flushCache));
65+
Runtime.getRuntime().addShutdownHook(newThread(cacheStore::flushCache));
6966
}
70-
CacheStore.clearCache();
71-
}
72-
73-
publicstaticvoidinitCacheCapacity(intcapacity) {
74-
CacheStore.initCapacity(capacity);
67+
cacheStore.clearCache();
7568
}
7669

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

106-
publicstaticStringprintCacheContent() {
107-
returnCacheStore.print();
99+
publicStringprintCacheContent() {
100+
returncacheStore.print();
108101
}
109102

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

118111
/**
119112
* Cache-Aside find user account helper.
120113
*/
121-
privatestaticUserAccountfindAside(StringuserId) {
122-
returnOptional.ofNullable(CacheStore.get(userId))
114+
privateUserAccountfindAside(StringuserId) {
115+
returnOptional.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
returnuserAccount;
127120
})
128121
.orElse(null);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp