Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 Object oriented ajax response caching

I'm working on a Laravel 5.1 project, using a lot of ajax calls returning html blocks.

To optimize the speed of the website i want to implement private and public response caching. this works fine using following code:

        return response()
        ->json($result)
        ->header('Cache-Control', 'public, max-age=300');

Yet using it this way wont hold in account objects that are updated within the 300 seconds.

Are there possibilities that allow me to clear the response cache of a request, if and only if the returning objects have been updated ?

like image 558
Frederiek Avatar asked Dec 19 '25 09:12

Frederiek


1 Answers

Maybe you can try server side caching with something like this below. sorry this is crude

function sometest(User $user)
{

    /** . . .conditions to check if some data has changed . . . **/


    $jsonResponse = Cache::remember(Auth::id() . "_sometest", 300, function () use ($user)
    {
        $result = $user->all(); //get result here

        return $result;
    });

    return response()->json($jsonResponse);
}

You can read about here Cache

you can also try

  • config caching: php artisan config:cache
  • route caching: php artisan route:cache
  • and utilizing memcached if you are able to.
like image 200
Zach Robichaud Avatar answered Dec 21 '25 23:12

Zach Robichaud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!