|
54 | 54 | */
|
55 | 55 | @Slf4j
|
56 | 56 | publicclassApp {
|
57 |
| -/** |
58 |
| - * Constant parameter name to use mongoDB. |
59 |
| - */ |
60 |
| -privatestaticfinalStringUSE_MONGO_DB ="--mongo"; |
61 |
| -/** |
62 |
| - * Application manager. |
63 |
| - */ |
64 |
| -privatefinalAppManagerappManager; |
65 |
| - |
66 |
| -/** |
67 |
| - * Constructor of current App. |
68 |
| - * @param isMongo boolean |
69 |
| - */ |
70 |
| -publicApp(finalbooleanisMongo) { |
71 |
| -DbManagerdbManager =DbManagerFactory.initDb(isMongo); |
72 |
| -appManager =newAppManager(dbManager); |
73 |
| -appManager.initDb(); |
74 |
| - } |
75 |
| - |
76 |
| -/** |
77 |
| - * Program entry point. |
78 |
| - * |
79 |
| - * @param args command line args |
80 |
| - */ |
81 |
| -publicstaticvoidmain(finalString[]args) { |
82 |
| -// VirtualDB (instead of MongoDB) was used in running the JUnit tests |
83 |
| -// and the App class to avoid Maven compilation errors. Set flag to |
84 |
| -// true to run the tests with MongoDB (provided that MongoDB is |
85 |
| -// installed and socket connection is open). |
86 |
| -Appapp =newApp(isDbMongo(args)); |
87 |
| -app.useReadAndWriteThroughStrategy(); |
88 |
| -StringsplitLine ="=============================================="; |
89 |
| -LOGGER.info(splitLine); |
90 |
| -app.useReadThroughAndWriteAroundStrategy(); |
91 |
| -LOGGER.info(splitLine); |
92 |
| -app.useReadThroughAndWriteBehindStrategy(); |
93 |
| -LOGGER.info(splitLine); |
94 |
| -app.useCacheAsideStategy(); |
95 |
| -LOGGER.info(splitLine); |
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(finalString[]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", |
157 |
| -"Adam", |
158 |
| -"He likes food."); |
159 |
| -varuserAccount4 =newUserAccount("004", |
160 |
| -"Rita", |
161 |
| -"She hates cats."); |
162 |
| -varuserAccount5 =newUserAccount("005", |
163 |
| -"Isaac", |
164 |
| -"He is allergic to mustard."); |
165 |
| - |
166 |
| -appManager.save(userAccount3); |
167 |
| -appManager.save(userAccount4); |
168 |
| -appManager.save(userAccount5); |
169 |
| -LOGGER.info(appManager.printCacheContent()); |
170 |
| -appManager.find("003"); |
171 |
| -LOGGER.info(appManager.printCacheContent()); |
172 |
| -UserAccountuserAccount6 =newUserAccount("006", |
173 |
| -"Yasha", |
174 |
| -"She is an only child."); |
175 |
| -appManager.save(userAccount6); |
176 |
| -LOGGER.info(appManager.printCacheContent()); |
177 |
| -appManager.find("004"); |
178 |
| -LOGGER.info(appManager.printCacheContent()); |
179 |
| - } |
180 |
| - |
181 |
| -/** |
182 |
| - * Cache-Aside. |
183 |
| - */ |
184 |
| -publicvoiduseCacheAsideStategy() { |
185 |
| -LOGGER.info("# CachingPolicy.ASIDE"); |
186 |
| -appManager.initCachingPolicy(CachingPolicy.ASIDE); |
187 |
| -LOGGER.info(appManager.printCacheContent()); |
188 |
| - |
189 |
| -varuserAccount3 =newUserAccount("003", |
190 |
| -"Adam", |
191 |
| -"He likes food."); |
192 |
| -varuserAccount4 =newUserAccount("004", |
193 |
| -"Rita", |
194 |
| -"She hates cats."); |
195 |
| -varuserAccount5 =newUserAccount("005", |
196 |
| -"Isaac", |
197 |
| -"He is allergic to mustard."); |
198 |
| -appManager.save(userAccount3); |
199 |
| -appManager.save(userAccount4); |
200 |
| -appManager.save(userAccount5); |
201 |
| - |
202 |
| -LOGGER.info(appManager.printCacheContent()); |
203 |
| -appManager.find("003"); |
204 |
| -LOGGER.info(appManager.printCacheContent()); |
205 |
| -appManager.find("004"); |
206 |
| -LOGGER.info(appManager.printCacheContent()); |
| 57 | +/** |
| 58 | + * Constant parameter name to use mongoDB. |
| 59 | + */ |
| 60 | +privatestaticfinalStringUSE_MONGO_DB ="--mongo"; |
| 61 | +/** |
| 62 | + * Application manager. |
| 63 | + */ |
| 64 | +privatefinalAppManagerappManager; |
| 65 | + |
| 66 | +/** |
| 67 | + * Constructor of current App. |
| 68 | + * @param isMongo boolean |
| 69 | + */ |
| 70 | +publicApp(finalbooleanisMongo) { |
| 71 | +DbManagerdbManager =DbManagerFactory.initDb(isMongo); |
| 72 | +appManager =newAppManager(dbManager); |
| 73 | +appManager.initDb(); |
| 74 | + } |
| 75 | + |
| 76 | +/** |
| 77 | + * Program entry point. |
| 78 | + * |
| 79 | + * @param args command line args |
| 80 | + */ |
| 81 | +publicstaticvoidmain(finalString[]args) { |
| 82 | +// VirtualDB (instead of MongoDB) was used in running the JUnit tests |
| 83 | +// and the App class to avoid Maven compilation errors. Set flag to |
| 84 | +// true to run the tests with MongoDB (provided that MongoDB is |
| 85 | +// installed and socket connection is open). |
| 86 | +Appapp =newApp(isDbMongo(args)); |
| 87 | +app.useReadAndWriteThroughStrategy(); |
| 88 | +StringsplitLine ="=============================================="; |
| 89 | +LOGGER.info(splitLine); |
| 90 | +app.useReadThroughAndWriteAroundStrategy(); |
| 91 | +LOGGER.info(splitLine); |
| 92 | +app.useReadThroughAndWriteBehindStrategy(); |
| 93 | +LOGGER.info(splitLine); |
| 94 | +app.useCacheAsideStategy(); |
| 95 | +LOGGER.info(splitLine); |
| 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(finalString[]args) { |
| 104 | +for (Stringarg :args) { |
| 105 | +if (arg.equals(USE_MONGO_DB)) { |
| 106 | +returntrue; |
| 107 | + } |
207 | 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", |
| 157 | +"Adam", |
| 158 | +"He likes food."); |
| 159 | +varuserAccount4 =newUserAccount("004", |
| 160 | +"Rita", |
| 161 | +"She hates cats."); |
| 162 | +varuserAccount5 =newUserAccount("005", |
| 163 | +"Isaac", |
| 164 | +"He is allergic to mustard."); |
| 165 | + |
| 166 | +appManager.save(userAccount3); |
| 167 | +appManager.save(userAccount4); |
| 168 | +appManager.save(userAccount5); |
| 169 | +LOGGER.info(appManager.printCacheContent()); |
| 170 | +appManager.find("003"); |
| 171 | +LOGGER.info(appManager.printCacheContent()); |
| 172 | +UserAccountuserAccount6 =newUserAccount("006", |
| 173 | +"Yasha", |
| 174 | +"She is an only child."); |
| 175 | +appManager.save(userAccount6); |
| 176 | +LOGGER.info(appManager.printCacheContent()); |
| 177 | +appManager.find("004"); |
| 178 | +LOGGER.info(appManager.printCacheContent()); |
| 179 | + } |
| 180 | + |
| 181 | +/** |
| 182 | + * Cache-Aside. |
| 183 | + */ |
| 184 | +publicvoiduseCacheAsideStategy() { |
| 185 | +LOGGER.info("# CachingPolicy.ASIDE"); |
| 186 | +appManager.initCachingPolicy(CachingPolicy.ASIDE); |
| 187 | +LOGGER.info(appManager.printCacheContent()); |
| 188 | + |
| 189 | +varuserAccount3 =newUserAccount("003", |
| 190 | +"Adam", |
| 191 | +"He likes food."); |
| 192 | +varuserAccount4 =newUserAccount("004", |
| 193 | +"Rita", |
| 194 | +"She hates cats."); |
| 195 | +varuserAccount5 =newUserAccount("005", |
| 196 | +"Isaac", |
| 197 | +"He is allergic to mustard."); |
| 198 | +appManager.save(userAccount3); |
| 199 | +appManager.save(userAccount4); |
| 200 | +appManager.save(userAccount5); |
| 201 | + |
| 202 | +LOGGER.info(appManager.printCacheContent()); |
| 203 | +appManager.find("003"); |
| 204 | +LOGGER.info(appManager.printCacheContent()); |
| 205 | +appManager.find("004"); |
| 206 | +LOGGER.info(appManager.printCacheContent()); |
| 207 | + } |
208 | 208 | }
|