@Andrew, all you need to do is defining a positive TTL for each route. F3 takes care of the rest for you.
Here's a small example to illustrate what's going on behind the scene when you define a TTL:
Without cache:
$f3->set('CACHE',FALSE);
$f3->route('GET /test',function(){
echo time();
},60);//ttl 60s
t=0: Browser1 requests the page. Server sends a fresh version. //Output: 0
t=10: Browser1 requests the page again with the header "If-Modified-Since: t=0". Server answers with a HTTP 304 status (Not modified), which means "use the browser cache". //Output: 0
t=20: Browser1 requests the page after cleaning up the browser cache. Server sends a fresh version. //Output: 20
t=30: Browser2 requests the page. Server sends a fresh version. //Output: 30
t=100: Browser2 requests the page again with the header "If-Modified-Since: t=30". Server sends a fresh version (no 304 since the TTL has expired). //Output: 100
With cache enabled:
$f3->set('CACHE',TRUE);
$f3->route('GET /test',function(){
echo time();
},60);//ttl 60s
t=0: Browser1 requests the page. Server sends a fresh version. //Output: 0
t=10: Browser1 requests the page again with the header "If-Modified-Since: t=0". Server answers with a HTTP 304 status (Not modified), which means "use the browser cache". //Output: 0
t=20: Browser1 requests the page after cleaning up the browser cache. Server sends the cached version (TTL not expired). //Output: 0
t=30: Browser2 requests the page. Server sends the cached version (TTL not expired). //Output: 0
t=100: Browser2 requests the page again with the header "If-Modified-Since: t=30". Server sends a fresh version (no 304 neither cached version since the TTL has expired). //Output: 100
t=0: Browser1 requests the page. Server sends a fresh version specifying Expires: t=60. //Output: 0
t=10: User requests the page again. Browser1 fetches it directly from cache (since TTL has not expired). //Output: 0
So once again, we just need to define route TTLs if we want pages to be cached. F3 handles the underlying HTTP:
- if CACHE=FALSE, it controls the browser cache.
- if CACHE=TRUE, it controls the browser AND the server cache.