barryvdh / laravel-httpcache

Laravel HTTP Cache
484 stars 41 forks source link

Cache Control Header #12

Open pchrysa opened 9 years ago

pchrysa commented 9 years ago

I have created an API with Laravel 5 and I want to setup HTTP Caching.

I have the following method:

public function respond($data)
{
    $response = response()->json($data, $this->getStatusCode());
    $response->setTtl(config('constants.CACHE_TIME'));
    return $response;
}

which uses your package

The data, as I suppose, are cached because when I run an API call with postman to be cached for a time, if I add dd('test') or anything in the method in the meantime, it will not run until the time I have set passes. However, postman shows me the following:

Age → 3 Cache-Control → no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public, s-maxage=50 Connection → Keep-Alive Content-Length → 44198 Content-Type → application/json Date → Thu, 11 Jun 2015 13:49:04 GMT Expires → Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive → timeout=5, max=99 Pragma → no-cache

How can I make Cache-Control not to return "no-store, no-cache, must-revalidate" and the Pragma not to be "no-cache"?

Any help will be appreciated.

Thank you in advance

PS: Will the package be updated to Laravel 5.1?

casperbakker commented 9 years ago

You can do that as follows:

$response->header('Cache-Control', 'no-store');
$response->header('Pragma', 'no-cache');

Just before the return.

barryvdh commented 9 years ago

Thanks Casper

@pchrysa It should work on L5.1. Do you have any problems? I recently tagged some releases for this and the dependancies, did that fix it?

pchrysa commented 9 years ago

No the issue still remains. And also if I set an expiration date it will have two dates. The default one and the one that I set eg:

Expires → Thu, 19 Nov 1981 08:52:00 GMT , Sat, 20 Jun 2015 08:52:00 GMT

casperbakker commented 9 years ago

@pchrysa What webserver do you use? Maybe the webserver is overriding your headers? Setting the headers in my example does really work with default Laravel 5.1 on default Nginx.

pchrysa commented 9 years ago

@cbakker I have setup an Apache server in Amazon S3