Cache a single post? Cache and pagination?

Published 1 year 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?

1 year ago (151,880 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

1 year ago (13,070 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.