|
54 | 54 | */
|
55 | 55 | @Slf4j
|
56 | 56 | public class App {
|
57 |
| - /** |
58 |
| - * Constant parameter name to use mongoDB. |
59 |
| - */ |
60 |
| - private static final String USE_MONGO_DB = "--mongo"; |
61 |
| - /** |
62 |
| - * Application manager. |
63 |
| - */ |
64 |
| - private final AppManager appManager; |
65 |
| - |
66 |
| - /** |
67 |
| - * Constructor of current App. |
68 |
| - * @param isMongo boolean |
69 |
| - */ |
70 |
| - public App(final boolean isMongo) { |
71 |
| - DbManager dbManager = DbManagerFactory.initDb(isMongo); |
72 |
| - appManager = new AppManager(dbManager); |
73 |
| - appManager.initDb(); |
74 |
| - } |
75 |
| - |
76 |
| - /** |
77 |
| - * Program entry point. |
78 |
| - * |
79 |
| - * @param args command line args |
80 |
| - */ |
81 |
| - public static void main(final String[] 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 |
| - App app = new App(isDbMongo(args)); |
87 |
| - app.useReadAndWriteThroughStrategy(); |
88 |
| - String splitLine = "=============================================="; |
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 |
| - private static boolean isDbMongo(final 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", |
157 |
| - "Adam", |
158 |
| - "He likes food."); |
159 |
| - var userAccount4 = new UserAccount("004", |
160 |
| - "Rita", |
161 |
| - "She hates cats."); |
162 |
| - var userAccount5 = new UserAccount("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 |
| - UserAccount userAccount6 = new UserAccount("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 |
| - public void useCacheAsideStategy() { |
185 |
| - LOGGER.info("# CachingPolicy.ASIDE"); |
186 |
| - appManager.initCachingPolicy(CachingPolicy.ASIDE); |
187 |
| - LOGGER.info(appManager.printCacheContent()); |
188 |
| - |
189 |
| - var userAccount3 = new UserAccount("003", |
190 |
| - "Adam", |
191 |
| - "He likes food."); |
192 |
| - var userAccount4 = new UserAccount("004", |
193 |
| - "Rita", |
194 |
| - "She hates cats."); |
195 |
| - var userAccount5 = new UserAccount("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 | + private static final String USE_MONGO_DB = "--mongo"; |
| 61 | + /** |
| 62 | + * Application manager. |
| 63 | + */ |
| 64 | + private final AppManager appManager; |
| 65 | + |
| 66 | + /** |
| 67 | + * Constructor of current App. |
| 68 | + * @param isMongo boolean |
| 69 | + */ |
| 70 | + public App(final boolean isMongo) { |
| 71 | + DbManager dbManager = DbManagerFactory.initDb(isMongo); |
| 72 | + appManager = new AppManager(dbManager); |
| 73 | + appManager.initDb(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Program entry point. |
| 78 | + * |
| 79 | + * @param args command line args |
| 80 | + */ |
| 81 | + public static void main(final String[] 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 | + App app = new App(isDbMongo(args)); |
| 87 | + app.useReadAndWriteThroughStrategy(); |
| 88 | + String splitLine = "=============================================="; |
| 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 | + private static boolean isDbMongo(final String[] args) { |
| 104 | + for (String arg : args) { |
| 105 | + if (arg.equals(USE_MONGO_DB)) { |
| 106 | + return true; |
| 107 | + } |
207 | 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", |
| 157 | + "Adam", |
| 158 | + "He likes food."); |
| 159 | + var userAccount4 = new UserAccount("004", |
| 160 | + "Rita", |
| 161 | + "She hates cats."); |
| 162 | + var userAccount5 = new UserAccount("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 | + UserAccount userAccount6 = new UserAccount("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 | + public void useCacheAsideStategy() { |
| 185 | + LOGGER.info("# CachingPolicy.ASIDE"); |
| 186 | + appManager.initCachingPolicy(CachingPolicy.ASIDE); |
| 187 | + LOGGER.info(appManager.printCacheContent()); |
| 188 | + |
| 189 | + var userAccount3 = new UserAccount("003", |
| 190 | + "Adam", |
| 191 | + "He likes food."); |
| 192 | + var userAccount4 = new UserAccount("004", |
| 193 | + "Rita", |
| 194 | + "She hates cats."); |
| 195 | + var userAccount5 = new UserAccount("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 | }
|
0 commit comments