PHPackages                             widmogrod/zf2-semi-http-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. widmogrod/zf2-semi-http-cache

ActiveLibrary

widmogrod/zf2-semi-http-cache
=============================

ZF2 HTTP Cache

2782[1 issues](https://github.com/widmogrod/zf2-semi-http-cache/issues)PHP

Since Apr 14Pushed 13y agoCompare

[ Source](https://github.com/widmogrod/zf2-semi-http-cache)[ Packagist](https://packagist.org/packages/widmogrod/zf2-semi-http-cache)[ RSS](/packages/widmogrod-zf2-semi-http-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Semi HTTP Cache for Zend Framework 2 [![Build Status](https://camo.githubusercontent.com/0116c3d833372cab14aca9f237c10cf0cf0222e6a1629d7ca2b2c669bebe9069/68747470733a2f2f7472617669732d63692e6f72672f7769646d6f67726f642f7a66322d73656d692d687474702d63616368652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/widmogrod/zf2-semi-http-cache)
===========================================================================================================================================================================================================================================================================================================================================================

[](#semi-http-cache-for-zend-framework-2-)

Exclamation
-----------

[](#exclamation)

This module is still in development phase.

Introduction
------------

[](#introduction)

### What issues it solves?

[](#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?

[](#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?

[](#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](http://symfony.com/doc/2.0/book/http_cache.html)

### Simple workflow

[](#simple-workflow)

1. 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`
2. 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.
3. 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 header `If-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.
4. 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
------------

[](#installation)

1. `cd my/project/directory`
2. Create a `composer.json` file with following content:

```
{
    "require": {
        "widmogrod/zf2-semi-http-cache": "dev-master"
    }
}
```

3. Run `php composer.phar install`
4. Open `my/project/folder/configs/application.config.php` and add `'WidHttpCache'` to your `'modules'` parameter.

Configuration
-------------

[](#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:

```
