Deciding how to cache is often much more complicated than writing the actual code, although the code can also be very complicated.
For any caching layer, you have to decide:
- storage? (on disk vs. in memory)
- host? (app server vs. database server vs. third-party like ElastiCache)
- what to cache? (DB query results and/or rendered views and/or DTOs)
- when to delete cache? (ex: row #12 was changed in the DB, so now I need to delete any cached data that should change because row #12 was an input for it)
- how to segment cache? (logged-in users may share some parts of cache, but not others)
- TTL for cache? (is it OK to just keep everything in memory forever, or does there need to be a time limit that will reduce the size of the cache?)
- max size for cache? (same issue as above, but the limit is the storage size instead of a proxy like time)
These seem like decent jumping off points:
https://alankent.me/2018/08/25/server-side-caching-strategie...
https://www.digitalocean.com/community/tutorials/web-caching...