Getting Work Done Through People

Getting People Done Through Work

Friday, May 14, 2010

eac_cache PHP caching/compression utility

On-the-fly compression and caching of PHP, html, JavaScript, Style sheets, XML, and other text-based files.

eac_cache.encode.php and eac_cache.control.php


Source Code: cache.encode

Source Code: cache.control

Documentation (12pg/304k)

Download eac_cache.zip

Interested in using this software in your project? Contact Kevin at KBurkholder@EarthAsylum.com
Creative Commons License This work is licensed under the Creative Commons GNU-LGPL License.
Licensing & Registration
Donations are greatly appreciated and help fund further development and maintain the LGPL licensing.
Follow me on Twitter for software updates, releases and announcements.


eac_cache is comprised of two PHP utility programs (eac_cache.encode.php and eac_cache.control.php) that manage caching and compression of PHP scripts and non-PHP text files (such as JavaScript, html, style sheets, or XML files).

The two programs act independently while being aware of and supporting each other when they are used concurrently.

Caching is managed both on the server and through browser directives (via http response headers) that optimize local and intermediary proxy caching.

Compression is managed on the server by creating a compressed version of a file whenever that file is first requested. Once the compressed version exists, it is used to respond to additional requests for the same file.

Caching and compression dramatically reduce the amount of data being sent from the server to the browser. Caching of compressed files on the server reduces or eliminates the processing time it takes to compress a file before sending it to the browser.

Sept 5, 2009 - minor update to better handle cached files when cookies are being set and issues with IE 6.0.28nn and compressed JavaScript files.


The primary focus of eac_cache.encode.php is on non-dynamic files requested from the web server such as html documents, JavaScript libraries, style sheet files, XML files, and/or other text-based files.

These files, when first requested, are compressed using the zlib compression format and stored on the server in a cache folder. When subsequent requests for the same file are received, the compressed version is delivered to the requesting client (browser).

eac_cache.encode.php will compress dynamic PHP script output but it will not cache the output for later use – the assumption being that the PHP script most likely generates different output for each request or, at least, must be executed independently for each request.


eac_cache.encode.php is a small utility script that manages zlib encoding (aka gzip compression) and caching for any file request handled by a web server. eac_cache.encode.php uses the standard zlib compression algorithms built in to PHP.

The “Content-Encoding” header in HTTP/1.1 allows clients to optionally receive compressed HTTP responses dramatically reducing the amount of data transmitted from the server to the browser. Almost all browsers support compressed server responses and eac_cache.encode.php respects the browser's "Accept-Encoding" header.

eac_cache.encode.php not only manages the compression of files, it also caches the compressed file in order to reduce or eliminate the overhead of compressing the file every time it is requested. Files are compressed and cached only once regardless of how many times they are requested.

As well, eac_cache.encode.php manages caching and content headers received from and returned to the browser for optimum browser caching effectiveness and compatibility.

Most PHP implementations support the automatic compression of PHP output. Optionally, the web server can be configured (through Apache, .htaccess, and/or php.ini) to pass non-PHP files through the PHP processor in order to compress non-PHP files.

# --- PHP processor uses zlib output compression --- #
php_flag zlib.output_compression On
php_value zlib
.output_compression_level 5
# --- make .css, .js, and .html pass through the PHP processor --- #
AddHandler application/x-httpd-php .css .js .html .htm 

However, this type of configuration is not always optimal and may not be supported by all browsers. ISPs may not enable these features or allow the web developer access to control these features. Additionally, non-PHP files are unnecessarily processed through the PHP interpreter.

eac_cache.encode.php is flexible in allowing it's use to be enabled according to the need and access rights of the web developer. eac_cache.encode.php can be used as:

  • An auto-prepend program configured in php.ini or an .htaccess file.
  • A URL prefix designated in .htaccess.
  • As a link href within a web page.
  • As a form post action.


Whereas eac_cache.encode.php focuses on non-dynamic content (i.e. non-PHP files), eac_cache.control.php is intended specifically for PHP scripts.

Very often a PHP script does not produce dynamic content or may not produce content that changes with every request.

In many cases, PHP scripts are created to execute something specific on the server (such as activity tracking or session management) yet the content delivered is no different than a static HTML page or may only change on a timed (i.e. hourly, daily or weekly) basis.


eac_cache.control.php is intended to be included in individual parent scripts after any initialization, tracking, and/or session management code has been executed but before any output is generated.

Its purpose is to cache the output similar to that of eac_cache.encode.php so that it may be used for subsequent requests while still allowing the initialization code to be executed with every request.

When eac_cache.control.php is executed, it captures the output from the parent script and saves it to a cache file. Subsequent requests will trigger the parent script to execute everything up to the include of eac_cache.control.php where eac_cache.control.php will deliver the cached version of the output and halt execution of the parent script.

eac_cache.control.php sends caching parameters to the browser that cause the browser to cache locally the page generated but to also verify that its cached version is up to date before using its cached version. So a request for a page is always sent to the server (except when using the browser’s back/forward buttons) and the server can always execute the script up to the point where eac_cache.control.php is included.

When eac_cache.control.php determines that the browser’s cached version is still current, it responds with an http 304 “Not Modified” header allowing the browser to use its cache. If the content is no longer up to date, the parent script is allowed to run, the output is saved in a new cache file, and the new version is sent to the browser.

By default, eac_cache.control.php determines that the browser’s cache is up to date based on the last modified time of the PHP script. Of course, with PHP this is not always the case. A script may be very old yet still produce new output. In order to override the default, a constant may be defined within the parent script that dictates the maximum age of the script output:

SCRIPT_CACHE_MAXAGE may have a number of values:

  • Typically the maximum age of the cached file in seconds.
    E.G. 3600 = 1 hour, 86400 = 1 day, 604800 = 1 week
  • A file name (either the complete path name or the path name relative to the web document root). In this case, the given file’s modification time is used instead of the script’s modification time when determining the “last-modified” date/time for caching.
    E.G. “/var/web/domain/http_docs/scripts/script.inc” or “/scripts/script.inc”
  • A relative date/time string indicating any time in the past.
    E.G. “yesterday”, “1am today” “00:00 last Sunday”
    see http://www.gnu.org/software/tar/manual/html_node/tar_119.html

 Powered by  eac::Framework 

eac::Framework is a lightweight PHP & JavaScript framework for Web 2.0 Applications and E-Commerce systems.

For more information, visit http://www.KevinBurkholder.com/framework

eac::encryption, eac::session, eac::keychain, eac::dataobjects, eac::tracker, eac::sourcing, eac::authentication, eac::filter, eac::formgen, eac::caching, eac::mailer, eac::download, eac::error, eac::streams and more.

Strengths Based Performance Management
EarthAsylum Consulting
The EarthAsylum Leadership Circle