cvs: pear /HTTP_Header/Header Cache.php

From: Date: Sat, 12 Jun 2004 18:51:25 +0000
Subject: cvs: pear /HTTP_Header/Header Cache.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20565@lists.php.net to get a copy of this message
mike Sat Jun 12 14:51:25 2004 EDT Modified files: /pear/HTTP_Header/Header Cache.php Log: * first cleanup * removing $_caching, because its pretty useless (if you use this package what else than caching is its purpose) * improved isCached() * send always Last-Modified

http://cvs.php.net/diff.php/pear/HTTP_Header/Header/Cache.php?r1=1.4&r2=1.5&ty=u Index: pear/HTTP_Header/Header/Cache.php diff -u pear/HTTP_Header/Header/Cache.php:1.4 pear/HTTP_Header/Header/Cache.php:1.5 --- pear/HTTP_Header/Header/Cache.php:1.4 Tue Dec 9 13:31:42 2003 +++ pear/HTTP_Header/Header/Cache.php Sat Jun 12 14:51:25 2004 @@ -2,7 +2,7 @@ // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ -// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group | +// | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | @@ -13,178 +13,169 @@ // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Wolfram Kriesing <wk@visionp.de> | -// | | -// +----------------------------------------------------------------------+// -// $Id: Cache.php,v 1.4 2003/12/09 18:31:42 cain Exp $ +// | Michael Wallner <mike@php.net> | +// +----------------------------------------------------------------------+ +// +// $Id: Cache.php,v 1.5 2004/06/12 18:51:25 mike Exp $ require_once 'HTTP/Header.php'; /** -* This package provides methods to easier handle caching of HTTP pages. -* That means that the pages can be cached at the client (User agent or browser) -* and your application only needs to send "hey client you already have the pages". -* Which is dont by sending the HTTP-Status 304 ('Not Modified'), so that your -* application load can be reduced and the net traffic too, since you only need -* to send the complete page once. This is really an advantage i.e. for generated -* style sheet, or simply pages that do only change rarely. -* -* I.e. when you dont want to send a client-side-cached page multiple times -* you can do this: -* <code> -* $httpCache = new HTTP_Header_Cache(); -* // if the page is cached, then we send a 304-Not modified header here and EXIT the code right here! -* // if not this method sets all the headers so that the page gets cached -* $httpCache->exitIfCached(); -* -* ...do the work that renders the real page, that shall be cached by the client... -* </code> -* -* Or when you know that the page shall only be cached for some time: -* <code> -* $httpCache = new HTTP_Header_Cache(); -* // check if the page the client has is older than 2 days -* if ($httpCache->isOlderThan(2,'days')) { -* $httpCache->sendHeaders(); // make sure that the headers, that tell this page shall be cached get sent -* ...generate and send all the cacheable content to the client... -* } else { -* $httpCache->exitIfCached(); -* } -* -* </code> -* -* @package HTTP_Header -* @author Wolfram Kriesing <wolfram@kriesing.de> -*/ + * HTTP_Header + * + * This package provides methods to easier handle caching of HTTP pages. That + * means that the pages can be cached at the client (user agent or browser) and + * your application only needs to send "hey client you already have the pages". + * + * Which is done by sending the HTTP-Status "304 Not Modified", so that your + * application load and the network traffic can be reduced, since you only need + * to send the complete page once. This is really an advantage e.g. for + * generated style sheet, or simply pages that do only change rarely. + * + * @package HTTP_Header + * @category HTTP + * @version $Revision: 1.5 $ + */ class HTTP_Header_Cache extends HTTP_Header { - - var $_caching = true; - /** - * - * - * - * @param boolean shall the - * @param integer the number of seconds after which the page expires - * 0 - is never - */ - function HTTP_Header_Cache($caching=true) - { - $this->_caching = $caching; - if ($this->_caching) { - $this->setHeader('Pragma','cache'); - $this->setHeader('Cache-Control','public'); + * Constructor + * + * Set the amount of time to cache. + * + * @access public + * @return object HTTP_Header_Cache + * @param int $expires + * @param string $unit + */ + function HTTP_Header_Cache($expires = 0, $unit = 'seconds') + { + $this->setHeader('Pragma', 'cache'); + $this->setHeader('Cache-Control', 'public'); + $this->setHeader('Last-Modified', $this->getCacheStart()); + + if ($expires && !$this->isOlderThan($expires, $unit)) { + $this->exitIfCached(); } } + /** + * Get Cache Start + * + * Returns the unix timestamp of the If-Modified-Since HTTP header or the + * current time if the header was not sent by the client. + * + * @access public + * @return int unix timestamp + */ function getCacheStart() { - return $this->dateToTimestamp($_SERVER['HTTP_IF_MODIFIED_SINCE']); + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + return strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])); + } + return time(); } /** - * You can call it like this: - * <code> - * $httpCache->isOlderThan(1,'day'); - * $httpCache->isOlderThan(47,'days'); - * - * $httpCache->isOlderThan(1,'week'); - * $httpCache->isOlderThan(3,'weeks'); - * - * $httpCache->isOlderThan(1,'hour'); - * $httpCache->isOlderThan(5,'hours'); - * - * $httpCache->isOlderThan(1,'minute'); - * $httpCache->isOlderThan(15,'minutes'); - * - * $httpCache->isOlderThan(1,'second'); - * $httpCache->isOlderThan(15); // is the same as isOlderThan(15,'seconds') - * </code> - * - * @param integer the number of units, if no second paramter given it means seconds - * @param string the unit, - * can be one of: 'week', 'weeks', 'day', 'days', 'hour', 'hours', 'minute', 'minutes', 'second' - * @return boolean true if it is older than what the parameters say - */ - function isOlderThan($time=0, $unit='') - { - switch (strtolower($unit)) { + * Is Older Than + * + * You can call it like this: + * <code> + * $httpCache->isOlderThan(1, 'day'); + * $httpCache->isOlderThan(47, 'days'); + * + * $httpCache->isOlderThan(1, 'week'); + * $httpCache->isOlderThan(3, 'weeks'); + * + * $httpCache->isOlderThan(1, 'hour'); + * $httpCache->isOlderThan(5, 'hours'); + * + * $httpCache->isOlderThan(1, 'minute'); + * $httpCache->isOlderThan(15, 'minutes'); + * + * $httpCache->isOlderThan(1, 'second'); + * $httpCache->isOlderThan(15); + * </code> + * + * If you sepcify something greater than "weeks" as untit, it just works + * approximatley, because a mount is taken to consist of 4.3 weeks. + * + * @access public + * @return bool Returns true if requested page is older than specified. + * @param int $time The amount of time. + * @param string $unit The unit of the time amount - (year[s], month[s], + * week[s], day[s], hour[s], minute[s], second[s]). + */ + function isOlderThan($time = 0, $unit = 'seconds') + { + static $cacheStart; + + if (!$time) { + return false; + } + + if (!isset($cacheStart)) { + $cacheStart = $this->getCacheStart(); + } + + switch (strtolower($unit)) + { + case 'year': + case 'years': + $time *= 12; + case 'month': + case 'months': + $time *= 4.3; case 'week': - case 'weeks': $time = $time*7; + case 'weeks': + $time *= 7; case 'day': - case 'days': $time = $time*24; + case 'days': + $time *= 24; case 'hour': - case 'hours': $time = $time*60; + case 'hours': + $time *= 60; case 'minute': - case 'minutes': $time = $time*60; - case 'second': break; - } - if ($time && $this->getCacheStart()+$time<time()) { - return true; + case 'minutes': + $time *= 60; + default: + $time += $cacheStart; } - return false; + + return $time < time(); } /** - * optionally you can pass an additional condition via parameter - * which is simply checked for true - * it would be the same as - * $this->isCached() && $condition === $this->isCached($condition) - * it is suggested to use the parameter since the handling for sending the - * 'last-modified' is included in this method here - * - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param boolean - */ - function isCached( $condition=true ) + * Is Cached + * + * @access public + * @return bool Whether the page/resource is considered to be cached. + * @param int $lastModified Unix timestamp of last modification. + */ + function isCached($lastModified = 0) { - if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $condition==true ) { - return true; + if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + return false; } - - // if the file is not cached and 'caching' is on - // then it shall be cached, and therefore we need to send - // the 'last-modified' header in order to get a 'if-modified-since' - // next time, so that we can answer with a 304 in case it is still cached - if ( $this->_caching ) { - $this->setHeader( 'Last-Modified' ); + if (!$lastModified) { + return true; } - return false; + return $lastModified > $this->getCacheStart(); } - - - + /** - * this method exits the script if the page is - * still cached by the user agent, the condition - * if given will be AND-ed to the 'isCached' call - * Since it returns false in case the page is not cached you can also use it in an - * if. for example: - * if (!$httpCache->exitIfCached()) { - * do stuff here in case it is not cached - * } - * - * @param boolean will be AND-ed to the 'isCached' call - */ - function exitIfCached( $condition=true ) + * Exit If Cached + * + * @access public + * @return void + */ + function exitIfCached() { - if ($this->isCached($condition)) { + if ($this->isCached()) { $this->sendHeaders(); $this->sendStatusCode(304); exit; } - return false; - } - - - - -/* ???? does that make sense??? 'must-revalidate' is one of many possible values for Cache-Control - we could check HTTP-compilance here, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html - function mustRevalidate( $yes=true ) - { - $this->setHeader('Cache-Control','must-revalidate'); } -*/ } ?>
« previous php.pear.cvs (#20565) next »