|
23 | 23 |
|
24 | 24 | package com.iluwatar.caching;
|
25 | 25 |
|
| 26 | +import com.iluwatar.caching.database.DbManager; |
| 27 | +import com.iluwatar.caching.database.DbManagerFactory; |
26 | 28 | import lombok.extern.slf4j.Slf4j;
|
27 | 29 |
|
28 | 30 | /**
|
|
55 | 57 | * <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i>
|
56 | 58 | * </p>
|
57 | 59 | *
|
| 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 | + * |
58 | 65 | * @see CacheStore
|
59 | 66 | * @see LruCache
|
60 | 67 | * @see CachingPolicy
|
61 | 68 | */
|
62 | 69 | @Slf4j
|
63 | 70 | 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 | + } |
165 | 194 | }
|
0 commit comments