2
votes

I have laravel (7.x) application. I recently added the cache functionality for the performance boost. After implementing the cache functionality, I was having trouble with the pagination while loading the data in grid format, so I googled for the solution and found this Pagination with cache in Laravel.

Although, it did solve my problem. But, the case is that I have about 100 pages and due to the solution I found, each page has it's own cache. Now, if I create or update any record then it doesn't reflect in the grid because the data is loaded from the cache.

PostController.php:

...

$arraySearch = request()->all();

# calculating selected tab
$cache = (!empty(request()->inactive)) ? 'inactive' : 'active';
$cacheKey = strtoupper("{$this->controller}-index-{$cache}-{$arraySearch['page']}");

# caching the fetch data
$arrayModels = cache()->remember($cacheKey, 1440, function() use ($arraySearch) {
    # models
    $Post = new Post();

    # returning
    return [
        'active'   => $Post->_index(1, 'active', $arraySearch),
        'inactive' => $Post->_index(0, 'inactive', $arraySearch),
    ];
});

...

Post.php:

public function _index($status = 1, $page = null, $arraySearch = null)
{
    ...

    $Self = self::where('status', $status)
        ->orderBy('status', 'ASC')
        ->orderBy('title', 'ASC')
        ->paginate(10);

    ...

    return $Self;
}

How do I clear all this cache to show the newly created or updated record to with the updated values.?

2
You need to clear the cache for all pages as soon as you edit a post. Or, if you can allow it, shorten the time the item is stored in the cache.Christophe Hubert
Ok, but could you please explain how do I do that..?MrSingh
Actually, the cache time is set by the client, so I can not change it.MrSingh

2 Answers

0
votes

1. Store All pages under the same tag:

As seen on the documentation: https://laravel.com/docs/master/cache#storing-tagged-cache-items You can use tags to group cached items.

    $cacheTag = strtoupper("{$this->controller}-index-{$cache}");
    $arrayModels = cache()->tags([$cacheTag])->remember($cacheKey, 1440, function() use ($arraySearch) {
        ...

2. Set an event listener on Post to clear the tag

You can run an Event listener on your Post update() or create() events. https://laravel.com/docs/7.x/eloquent#events-using-closures

You can then clear the tag cache using

    Cache::tags([$cacheTag])->flush();
0
votes

I know this isn't the proper solution. But, until I find the proper way to do it, this is the option I am kind of stuck with.

PostController.php:

public function index()
{
    ...

    $arraySearch = request()->all();

    # calculating selected tab
    $cache = (!empty(request()->inactive)) ? 'inactive' : 'active';
    $cacheKey = strtoupper("{$this->controller}-index-{$cache}-{$arraySearch['page']}");

    # caching the fetch data
    $arrayModels = cache()->remember($cacheKey, 1440, function() use ($arraySearch) {
        # models
        $Post = new Post();

        # returning
        return [
            'active'   => $Post->_index(1, 'active', $arraySearch),
            'inactive' => $Post->_index(0, 'inactive', $arraySearch),
        ];
    });

    ...
}

public function store()
{
    ...

    Artisan::call('cache:clear');

    ...
}

I'll post the proper solution when I find one. Till then I am using this one.