Cache a single post? Cache and pagination?

Published 9 months ago by ronon

I have two question related to Laravels Caching System.

First: Is it possible to Cache a single post?

class PostController{
    
    public function show(Post $post){
        // Post need to be cached before it arrives here?!?
    }
}

On index I can do.

$posts = Cache:rembemer('posts', 60, function(){
    return Post::all();
});

Get all posts if they are not in cache.

Second: If you execute the code from above, can you still use pagination on posts, or do you have to cache every page from pagination?

Dry7
Dry7
9 months ago (151,680 XP)

@ronon first:

$id = 1;
$post = Cache:rembemer('posts.' . $id, 60, function() use ($id) {
    return Post::find($id);
});

second: no, you need to make separate requests for each page

ronon
ronon
9 months ago (12,640 XP)

Correct me if i'm wrong, but $post in the show(Post $post) method was already executed, so wouldn't your attempt be obsolete, or did you place it somewhere else as in PostController?

Please sign in or create an account to participate in this conversation.