Design 1: For a web application. In the past, if I insert a record/data in UI, I will save this new data to the database, then the UI will be reloaded (like clicking browser refresh icon), but this UI reloaded action will query the database again to fetch all data into UI. I know this will slow the web application performance.
Design 2: Now, I heard this 'caching' idea. When we run the web application at the beginning, I load all data which is fetching from the database into a variable (Let's say 'CacheVariable', so this variable contains all the data I need for the whole UI of the web application). Now, If I insert a new record/data in UI, I will still need to insert this record/data to the database. But now, instead of reloading UI to get updated data from the database. I just insert this new data/record into 'CacheVariable', so my UI can get the updated data without reloading page. The problem is, every time, when I do CRUD actions, I need to remember to update 'CacheVariable', otherwise, I will get the UI bugs. Let's say, I have 100 fields in my UI, then every field has its own CRUD actions. This is I need to do 400 (100 fields * 4) times operations on 'CacheVariable', which I think is not a good way.
Disadvantages of Design 2:
- If I refactored my 'CacheVariable', I also need to refactored 400 times for all CRUD on 'CacheVariable'.
- Since every field needs CRUD actions on 'CacheVariable', sometimes I will forget to do that, which will cause UI bug. It also will be an error-prone idea since I have to do 400 times.