Rjs37's avatar

Unusual Pagination Caching Problem

I'm getting a weird caching problem due to the way the paginator seems to generate the urls before it is even rendered.

In my project I'm using a custom private package that is included for both our public and admin websites (they live as two separate laravel apps on the same server).

I'm using repositories with caching wrapped around for example:

// Eloquent Repository
public function findAll($perpage = 12)
    {
        return $this->model->with('author.profile')->orderBy('created_at', 'desc')->paginate($perpage);
    }
// Cache Repository
public function findAll($perpage = 12)
    {
        $key = 'articles.all.'.Input::get('page', 1).'.'.$perpage;

        return $this->cache->tags('allarticles')->remember($key, $this->minutes, function () use ($perpage) {
        
            return $this->articles->findAll($perpage);
        });
    }

The problem I've noticed is that the paginator is generating the full url for the pages during the paginate call, this url is then included as part of the cache and thus is used on both the public and admin sites during the lifetime of that cache. So I can either end up with admin links on the public site or the opposite.

A possible solution (due to how I've setup the cache key) would be to use different page sizes for the admin and public sites but that seems like such a hack.

I'm kind of surprised it's generating the html and links this early. Is there a way of making the paginator use relative urls rather than generating the full url before it even reaches the view?

EDIT: I'm currently on Laravel 5.1, in-case that helps.

0 likes
1 reply
Rjs37's avatar

For the time being I've gone with the dirty fix of using different page sizes for the admin.

Another option would be not using the cache at all for the admin area (shouldn't technically need it I guess but was useful for the admin to see exactly same as the user). And actions in the admin have to interact with the cache in terms of flushing etc.

I just find it weird that the paginator is generating the links at that point rather than when it's being rendered.

Please or to participate in this conversation.