grundig1313's avatar

Cache dynamic / user related content

Hi,

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?

Thanks for any help! Simon

0 likes
2 replies
tekmi's avatar

Hi @grundig1313

Maybe you should take a look at this series https://laracasts.com/series/learn-laravel-and-redis-through-examples - especially the lessons with counters and sorted sets - it may give you some inspiration, or at least the different look at your problem.

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.

Good luck.

grundig1313's avatar

Hi and thank you!

I've already checked this series but it's more about the "featured, trending, ..." posts problem.

Trending posts are the same for all of my users. So it's all about finding a good way to assign user likes to this cached post list.

Simon

Please or to participate in this conversation.