ronon's avatar
Level 9

Cache a single post? Cache and pagination?

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?

0 likes
2 replies
Dry7's avatar

@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's avatar
Level 9

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 or to participate in this conversation.