I'm currently trying to introduce cache into a project. To make the example simple I will use an example of a blog having categories and posts. A category can have multiple posts assigned, and a post can be assigned to multiple categories.
I'm doubting between the following two strategies:
Strategy A:
- Caching the single posts
- Caching the complete list of posts for specific lists (e.g. posts by category, top-10, etc. etc.)
(to keep the example simple let's ignore pagination here)
Strategy B:
- Caching the single posts (exactly like with strategy A)
- For lists of posts (e.g. by category, top-10, etc. etc.) I ONLY cache the ids of the posts.
Retrieving a list would work as follows:
Create an empty array for the posts. Loop the post-ids for the list and call the find-method to retrieve a single post (this function would return the cached post when available of course).
Then add the returned post to the array of posts and finally return the array of posts.
My feeling tends to go towards strategy B because of the following reasons:
There are no duplicates in the cache. A post will only exist once in the cache (as a single item).
A post (except its non-changing id) will not exist in the lists. This would save storage space in the cache, which sounds welcoming when using in-memory cache drivers like Redis (RAM = expensive)
When a post changes I only have to invalidate and update the single cached post. All lists will still remain the same as they only contain the ids.
It would make pagination completely independent of the database (also saving the need for paginated queries) because I can slice the list of ids myself.
A downside could be that the initial request needs more database-queries (as all posts are queried independently because of the list of ids and the loop to create the output for a list)
But this would only be the case for the first load because after that the (most) content will come from the cache.
Maybe there's a name for this method of caching, but I was unable to find anything about this online, making me doubt if this would a good approach at all.
I'm very curious about your opinions or possible alternatives that maybe are (even) more efficient.