Do you have any good information about what is actually slow? Is loading the entire thread slow? Or just adding the replies? My reason for asking is that you might be prematurely caching information which isn't very slow at all. It might make more sense to cache the individual replies and not worry about caching the pagination at all—but rather focus on caching the eagerly loaded objects.
Jun 18, 2018
2
Level 13
Cache Records Before pagination
I've a thread model which 'hasMany' replies. Now, in order to render a thread with all the replies, I'm fetching thread and replies separately.
I'm trying to use the following logic to cache replies to any thread -
$replies = Cache::store('redis')->tags(['thread_' . $thread->id . '_replies'])->rememberForever('thread_' . $thread->id . '_replies_' . $page, function () use($thread) {
return $thread->replies()->with(['owner', 'likes'])->withCount('likes')->paginate(20);
});
However, with this approach, I've to cache each page separately. This makes it difficult to flush cache when a reply is posted or an existing reply is liked. I was thinking if there was a way to store all the replies to a thread in cache, and paginate them after retrieving.
Is there any way to do that? Would really appreciate your help.
Please or to participate in this conversation.