widmogrod / zf2-semi-http-cache
ZF2 HTTP Cache
Requires
- php: >=5.3
Requires (Dev)
- phpunit/phpunit: 3.7.*
- zendframework/zendframework: 2.1.*
This package is auto-updated.
Last update: 2024-12-07 19:12:32 UTC
README
Exclamation
This module is still in development phase.
Introduction
What issues it solves?
- Wrong
Last-Modified
date when your application is running on Apache. This date in taken from index.php modification date which is not accurate and can cause that browser won't cache response.
What benefits it brings?
Enabling browser cache workflow.
- Handling
If-Modified-Since
which:- Reducing bandwidth. If browser has valid cached data then only 304 header is send.
- Speed up response time, by omitting dispatch event if browser cache is not stale.
- Providing out of the box
Cache-Control
management. - Providing more accurate
Last-Modified
date solution (but not perfect).
Why semi
HTTP cache?
Because things like If-Modified-Since
are calculated base of Cache-Control: max-age
and not base to real modification date of requested entity.
The max-age
is set explicit per action or global for application.
It's big simplification, but as solution out of the box is quite effective.
If you looking for something more bespoke I recommend The Symfony framework approach:
http://symfony.com/doc/2.0/book/http_cache.html
Simple workflow
-
Browser request resource
/data.json
. In application max-age is set for this resource for 60 seconds. Application return response with headers:Last-Modified: 2013-04-01 10:00:00 GMT
Cache-Control: max-age=60
-
a) Browser try to request resource
/data.json
after 10 seconds, but it already has cached content so it return data from browser cache. No request is made. -
b) Sometimes may happen that browser try to request resource
/data.json
after 10 seconds, and it want to validate if cached resource is stale. So it sends request wth headerIf-Modified-Since : 2013-04-01 10:00:00 GMT
. But application knows that valid lifetime for this resource is 60 seconds so application return response without body but with headers:304 Not Modified
Last-Modified: 2013-04-01 10:00:00 GMT
- note that last modified is old one.
-
Browser try to request resource after 70 seconds, but it already has cached content so it sends request with header
If-Modified-Since : 2013-04-01 10:00:00 GMT
to validate if cached resource is stale. Application knows that browser cache is stage so it's returning respond with headers and with new response body:Last-Modified: 2013-04-01 10:01:10 GMT
- note new last modified time.Cache-Control: max-age=60
Installation
cd my/project/directory
- Create a
composer.json
file with following content:
{ "require": { "widmogrod/zf2-semi-http-cache": "dev-master" } }
- Run
php composer.phar install
- Open
my/project/folder/configs/application.config.php
and add'WidHttpCache'
to your'modules'
parameter.
Configuration
By default, HTTP Cache is disabled if you wan to enable it you should copy configuration file:
cp vendor/widmogrod/zf2-semi-http-cache/config/zf2-semi-http-cache.local.php config/autoload/
Or, add following config entry to your local.php configuration file:
<?php return array( 'zf2-semi-http-cache' => array( 'enabled' => true, 'default' => array( 'max-age' => 600, // 10min in browser ), ), );
Todo
- create simple UML diagram describing workflow
- example with Varnish cache.
Time saving tips & tricks
Does your application use session?
- Remember to start session in action like login, do not do it every time.
- If session is started, headers like Set-Cookie & Cache-Control can be send. To avoid caching headers in Varnish like Set-Cookie best decision will be to set:
session_cache_limiter('no-cache'); session_cache_expire(0);