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

Commit73eca64

Browse files
authored
refactoring: Issue-1694: Improve caching pattern code quality (iluwatar#1810)
* Changed database implementation. Removed static objects.* Fix Logs* Fix 40 errors from checkstyle plugin run. 139 left))* Fix CacheStore errors from checkstyle plugin 107 left* Fix last errors in checkstyle.* Fix sonar issues* Fix issues in VALIDATE phase* Fix Bug with mongo connection. Used "Try with resources"* Add test* Added docker-compose for mongo db. MongoDb db work fixed.* Provided missing tests* Comments to start Application with mongo.* Fixes according PR comments. Mainly Readme edits.* Remove duplicated imports
1 parent8aac45a commit73eca64

22 files changed

+938
-468
lines changed

‎caching/README.md

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,29 @@ Wikipedia says:
4343
**Programmatic Example**
4444

4545
Let's first look at the data layer of our application. The interesting classes are`UserAccount`
46-
which is a simple Java object containing the user account details, and`DbManager` which handles
47-
reading and writing of these objects to/fromMongoDBdatabase.
46+
which is a simple Java object containing the user account details, and`DbManager`interfacewhich handles
47+
reading and writing of these objects to/from database.
4848

4949
```java
50-
@Setter
51-
@Getter
50+
@Data
5251
@AllArgsConstructor
5352
@ToString
53+
@EqualsAndHashCode
5454
publicclassUserAccount {
5555
privateString userId;
5656
privateString userName;
5757
privateString additionalInfo;
5858
}
5959

60-
@Slf4j
61-
publicfinalclassDbManager {
62-
63-
privatestaticMongoClient mongoClient;
64-
privatestaticMongoDatabase db;
65-
66-
privateDbManager() {/*...*/ }
67-
68-
publicstaticvoidcreateVirtualDb() {/*...*/ }
69-
70-
publicstaticvoidconnect()throwsParseException {/*...*/ }
71-
72-
publicstaticUserAccountreadFromDb(StringuserId) {/*...*/ }
60+
publicinterfaceDbManager {
7361

74-
publicstaticvoidwriteToDb(UserAccountuserAccount) {/*...*/ }
75-
76-
publicstaticvoidupdateDb(UserAccountuserAccount) {/*...*/ }
77-
78-
publicstaticvoidupsertDb(UserAccountuserAccount) {/*...*/ }
62+
voidconnect();
63+
voiddisconnect();
64+
65+
UserAccountreadFromDb(StringuserId);
66+
UserAccountwriteToDb(UserAccountuserAccount);
67+
UserAccountupdateDb(UserAccountuserAccount);
68+
UserAccountupsertDb(UserAccountuserAccount);
7969
}
8070
```
8171

@@ -171,30 +161,43 @@ strategies.
171161
@Slf4j
172162
publicclassCacheStore {
173163

164+
privatestaticfinalintCAPACITY=3;
174165
privatestaticLruCache cache;
166+
privatefinalDbManager dbManager;
175167

176168
/* ... details omitted ...*/
177169

178-
publicstaticUserAccountreadThrough(StringuserId) {
170+
publicUserAccountreadThrough(finalStringuserId) {
179171
if (cache.contains(userId)) {
180-
LOGGER.info("#Cache Hit!");
172+
LOGGER.info("#Found in Cache!");
181173
return cache.get(userId);
182174
}
183-
LOGGER.info("#Cache Miss!");
184-
UserAccount userAccount=DbManager.readFromDb(userId);
175+
LOGGER.info("#Not found in cache! Go to DB!!");
176+
UserAccount userAccount=dbManager.readFromDb(userId);
185177
cache.set(userId, userAccount);
186178
return userAccount;
187179
}
188180

189-
publicstaticvoidwriteThrough(UserAccountuserAccount) {
181+
publicvoidwriteThrough(finalUserAccountuserAccount) {
190182
if (cache.contains(userAccount.getUserId())) {
191-
DbManager.updateDb(userAccount);
183+
dbManager.updateDb(userAccount);
192184
}else {
193-
DbManager.writeToDb(userAccount);
185+
dbManager.writeToDb(userAccount);
194186
}
195187
cache.set(userAccount.getUserId(), userAccount);
196188
}
197189

190+
publicvoidwriteAround(finalUserAccountuserAccount) {
191+
if (cache.contains(userAccount.getUserId())) {
192+
dbManager.updateDb(userAccount);
193+
// Cache data has been updated -- remove older
194+
cache.invalidate(userAccount.getUserId());
195+
// version from cache.
196+
}else {
197+
dbManager.writeToDb(userAccount);
198+
}
199+
}
200+
198201
publicstaticvoidclearCache() {
199202
if (cache!=null) {
200203
cache.clear();
@@ -225,34 +228,39 @@ class.
225228
publicfinalclassAppManager {
226229

227230
privatestaticCachingPolicy cachingPolicy;
231+
privatefinalDbManager dbManager;
232+
privatefinalCacheStore cacheStore;
228233

229234
privateAppManager() {
230235
}
231236

232-
publicstaticvoidinitDb(booleanuseMongoDb) {/* ...*/ }
237+
publicvoidinitDb() {/* ...*/ }
233238

234239
publicstaticvoidinitCachingPolicy(CachingPolicypolicy) {/* ...*/ }
235240

236241
publicstaticvoidinitCacheCapacity(intcapacity) {/* ...*/ }
237242

238-
publicstaticUserAccountfind(StringuserId) {
239-
if (cachingPolicy==CachingPolicy.THROUGH|| cachingPolicy==CachingPolicy.AROUND) {
240-
returnCacheStore.readThrough(userId);
243+
publicUserAccountfind(finalStringuserId) {
244+
LOGGER.info("Trying to find {} in cache", userId);
245+
if (cachingPolicy==CachingPolicy.THROUGH
246+
|| cachingPolicy==CachingPolicy.AROUND) {
247+
return cacheStore.readThrough(userId);
241248
}elseif (cachingPolicy==CachingPolicy.BEHIND) {
242-
returnCacheStore.readThroughWithWriteBackPolicy(userId);
249+
returncacheStore.readThroughWithWriteBackPolicy(userId);
243250
}elseif (cachingPolicy==CachingPolicy.ASIDE) {
244251
return findAside(userId);
245252
}
246253
returnnull;
247254
}
248255

249-
publicstaticvoidsave(UserAccountuserAccount) {
256+
publicvoidsave(finalUserAccountuserAccount) {
257+
LOGGER.info("Save record!");
250258
if (cachingPolicy==CachingPolicy.THROUGH) {
251-
CacheStore.writeThrough(userAccount);
259+
cacheStore.writeThrough(userAccount);
252260
}elseif (cachingPolicy==CachingPolicy.AROUND) {
253-
CacheStore.writeAround(userAccount);
261+
cacheStore.writeAround(userAccount);
254262
}elseif (cachingPolicy==CachingPolicy.BEHIND) {
255-
CacheStore.writeBehind(userAccount);
263+
cacheStore.writeBehind(userAccount);
256264
}elseif (cachingPolicy==CachingPolicy.ASIDE) {
257265
saveAside(userAccount);
258266
}
@@ -272,24 +280,35 @@ Here is what we do in the main class of the application.
272280
@Slf4j
273281
publicclassApp {
274282

275-
publicstaticvoidmain(String[]args) {
276-
AppManager.initDb(false);
277-
AppManager.initCacheCapacity(3);
278-
var app=newApp();
283+
publicstaticvoidmain(finalString[]args) {
284+
boolean isDbMongo= isDbMongo(args);
285+
if(isDbMongo){
286+
LOGGER.info("Using the Mongo database engine to run the application.");
287+
}else {
288+
LOGGER.info("Using the 'in Memory' database to run the application.");
289+
}
290+
App app=newApp(isDbMongo);
279291
app.useReadAndWriteThroughStrategy();
292+
String splitLine="==============================================";
293+
LOGGER.info(splitLine);
280294
app.useReadThroughAndWriteAroundStrategy();
295+
LOGGER.info(splitLine);
281296
app.useReadThroughAndWriteBehindStrategy();
297+
LOGGER.info(splitLine);
282298
app.useCacheAsideStategy();
299+
LOGGER.info(splitLine);
283300
}
284301

285302
publicvoiduseReadAndWriteThroughStrategy() {
286303
LOGGER.info("# CachingPolicy.THROUGH");
287-
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
304+
appManager.initCachingPolicy(CachingPolicy.THROUGH);
305+
288306
var userAccount1=newUserAccount("001","John","He is a boy.");
289-
AppManager.save(userAccount1);
290-
LOGGER.info(AppManager.printCacheContent());
291-
AppManager.find("001");
292-
AppManager.find("001");
307+
308+
appManager.save(userAccount1);
309+
LOGGER.info(appManager.printCacheContent());
310+
appManager.find("001");
311+
appManager.find("001");
293312
}
294313

295314
publicvoiduseReadThroughAndWriteAroundStrategy() {/* ...*/ }
@@ -300,16 +319,6 @@ public class App {
300319
}
301320
```
302321

303-
Finally, here is some of the console output from the program.
304-
305-
```
306-
12:32:53.845 [main] INFO com.iluwatar.caching.App - # CachingPolicy.THROUGH
307-
12:32:53.900 [main] INFO com.iluwatar.caching.App -
308-
--CACHE CONTENT--
309-
UserAccount(userId=001, userName=John, additionalInfo=He is a boy.)
310-
----
311-
```
312-
313322
##Class diagram
314323

315324
![alt text](./etc/caching.png"Caching")

‎caching/docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version:'3.7'
2+
services:
3+
mongodb_container:
4+
image:mongo:latest
5+
environment:
6+
MONGO_INITDB_ROOT_USERNAME:root
7+
MONGO_INITDB_ROOT_PASSWORD:rootpassword
8+
ports:
9+
-27017:27017
10+
volumes:
11+
-./mongo-data/:/data/db

‎caching/etc/caching.png

-112 KB
Binary file not shown.

‎caching/pom.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@
3838
<scope>test</scope>
3939
</dependency>
4040
<dependency>
41-
<groupId>org.mongodb</groupId>
42-
<artifactId>mongodb-driver</artifactId>
43-
<version>3.12.1</version>
44-
</dependency>
45-
<dependency>
46-
<groupId>org.mongodb</groupId>
47-
<artifactId>mongodb-driver-core</artifactId>
48-
<version>3.0.4</version>
41+
<groupId>org.mockito</groupId>
42+
<artifactId>mockito-all</artifactId>
43+
<version>1.10.19</version>
44+
<scope>test</scope>
4945
</dependency>
5046
<dependency>
5147
<groupId>org.mongodb</groupId>
52-
<artifactId>bson</artifactId>
53-
<version>3.0.4</version>
48+
<artifactId>mongo-java-driver</artifactId>
5449
</dependency>
5550
</dependencies>
5651
<!--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp