Jump to content

Recommended Posts

Hi

I am developing a rest API using PHP and returning a ison response, I am setting the headers of the response as below:

 

header('Content-type:application/json;charset=utf-8');
header('Cache-Control: public, max-age=60');

 

However if I use Postman or Google Chrome or Safari or any web browser the response isn't caching.

the url is something like https://www.mysite.com/contacts

and it responds with a json response of all contacts

What I am expecting is that response to be cached for 60 seconds, but if I update the table in the database from the backend and then refresh/press enter on the address bar to re-load I get the new value straight away. I am expecting the same value to be returned until the 60 seconds has lapsed.

Any idea where I am going wrong?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/307330-json-response-not-caching/
Share on other sites

There are two basic forms of caching. The one you're using involves Cache-Control and headers like ETag or Last-Modified, where the browser will submit additional requests to your server but you can conditionally tell it that a cached copy is still valid.

GET /foo HTTP/1.1
Host: example.com

HTTP/1.1 200 OK
Cache-Control: public, max-age=60
ETag: abc123
...

GET /foo HTTP/1.1
Host: example.com
If-None-Match: abc123

HTTP/1.1 304 Not Modified
Cache-Control: public, max-age=60
ETag: abc123

ETag gives an identifier and If-None-Match is how the browser indicates that it has a cached copy but still wants to check with you in case the resource has changed. Alternatively there's Last-Modified (a date) and the corresponding If-Modified-Since. Or you can support both.

 

The other form is with Expires. It's older and more forceful about caching where the browser typically won't even try to check if the resource has changed. This is probably what you're expecting, however the above mechanism is smarter so you should use it instead.

GET /foo HTTP/1.1
Host: example.com

HTTP/1.1 200 OK
Expires: Fri, 01 Jun 2018 00:00:00 UTC
...

Expires and Cache-Control don't mix.

  • Great Answer 1

How exactly are you testing this to determine it's not caching?  Development tools for example may ignore caching as usually you don't want something to be cached when your developing on it.  Make sure whatever means you're using to test your system actually supports caching. 

You mention for example pressing refresh/reload in the browser.  That sort of action is exactly what you do to tell the browser to ignore it's cache and fetch a new copy of the resource.

Hi

Using the method of expires (to see if i can get it working) the Response headers are returning an Expires with a date in the future (around 5 minutes to test). However when I refresh (either by clicking the refresh or by just pressing enter on the address bar (to reload the page) it seems to give the new data (that I changed in the database using the back end).

Expected results is to return the same data until the expiry has passed and then on the next reload to bring the new data back.

 

How should I be testing if it is being cached?

You can't force the browser to reuse its cached copy every time. All you can do is instruct it that it can.

Instead of refreshing or using the address bar, try links between pages. Put a link to the JSON response in one page, then use that link.

  • Great Answer 1
3 hours ago, requinix said:

You can't force the browser to reuse its cached copy every time. All you can do is instruct it that it can.

Instead of refreshing or using the address bar, try links between pages. Put a link to the JSON response in one page, then use that link.

Perfect!

using a link to the json page and then using the backwards button and clicking the link again causes it to use be cached page, if you continue just clicking the link and then pressing back over and over until the cache time expires, on the next load it pulls the new data. This is exactly what I was expecting and it working ok using this method.

 

thank you for your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.