I have a blog where I have to display a few lists (trending posts, top posts, ...).
Authenticated users can like a post. As the queries for the above mentioned "lists" are a bit complicated / performance consuming, I cache the query results.
But what about the user likes? What would be a good way to cache these likes? I could add the userId to the cache key, but than I would have a lot of cached results with almost the same content (only the user likes would vary) and every time a user likes / dislikes a post, I would have to flush the cache.
What about storing the posts in cache (without the user like relation), fetching the likes on every request and then assigning each like to the corresponding post (if it exists in the list)? Is there a better way to accomplish this?
You could either increment the counter of key posts.{id} or even add up users ids under this key - in case you wanted to know who liked given post.
Obviously this solution will increase the complexity of your deployment, because besides the relational database, you would need to maintain a key-value database, so pick up the simplest solution for you.