|
23 | 23 |
|
24 | 24 | packagecom.iluwatar.caching;
|
25 | 25 |
|
| 26 | +importcom.iluwatar.caching.database.DbManager; |
| 27 | +importcom.iluwatar.caching.database.DbManagerFactory; |
26 | 28 | importlombok.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 | 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 | + } |
165 | 194 | }
|