Even if we have a simple application and we use a simple hashmap as a memory cache we still have to address this issue. When we update a database the cache should be updated with the new data or the old data should be removed from the cache.
Each time we update something in the database we have to remove the updated entities from the cache. We should take a special care because we our changes might affect other entities kept in cache:
update database
invalidate saved and affected entities from cache
As I said web applications are special applications. For each http request a thread will be created. In order to make sure that everything will work fine we must ensure that:
The applications where memory cache is required are the applications with many users and usually they run in distributed environments. There are options here: