mds1256 Posted May 30, 2018 Share Posted May 30, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/ Share on other sites More sharing options...
requinix Posted May 31, 2018 Share Posted May 31, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558696 Share on other sites More sharing options...
mds1256 Posted May 31, 2018 Author Share Posted May 31, 2018 Even if I use Expires it still doesn't seem to cache using anything even though the header is sent in the response. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558699 Share on other sites More sharing options...
requinix Posted May 31, 2018 Share Posted May 31, 2018 Are you sure it's sent? Have you verified with a browser that it's sent, with a date in the future, and that a subsequent normal page load by the browser doesn't request the response again? And you're using GET? Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558701 Share on other sites More sharing options...
kicken Posted May 31, 2018 Share Posted May 31, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558704 Share on other sites More sharing options...
mds1256 Posted June 1, 2018 Author Share Posted June 1, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558712 Share on other sites More sharing options...
requinix Posted June 2, 2018 Share Posted June 2, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558721 Share on other sites More sharing options...
mds1256 Posted June 2, 2018 Author Share Posted June 2, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307330-json-response-not-caching/#findComment-1558723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.