cvs: pear /HTTP_Header/Header Cache.php

From: Date: Fri, 17 Jan 2003 15:35:41 +0000
Subject: cvs: pear /HTTP_Header/Header Cache.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8342@lists.php.net to get a copy of this message
cain Fri Jan 17 10:35:41 2003 EDT Modified files: /pear/HTTP_Header/Header Cache.php Log: - add methods to check cache times - add some comments Index: pear/HTTP_Header/Header/Cache.php diff -u pear/HTTP_Header/Header/Cache.php:1.1.1.1 pear/HTTP_Header/Header/Cache.php:1.2 --- pear/HTTP_Header/Header/Cache.php:1.1.1.1 Fri Jan 17 03:57:24 2003 +++ pear/HTTP_Header/Header/Cache.php Fri Jan 17 10:35:41 2003 @@ -15,7 +15,7 @@ // | Authors: Wolfram Kriesing <wk@visionp.de> | // | | // +----------------------------------------------------------------------+// -// $Id: Cache.php,v 1.1.1.1 2003/01/17 08:57:24 cain Exp $ +// $Id: Cache.php,v 1.2 2003/01/17 15:35:41 cain Exp $ require_once 'HTTP/Header.php'; @@ -42,18 +42,47 @@ { $this->_caching = $caching; - if( $this->_caching ) { + if ( $this->_caching ) { $this->setHeader('Pragma','cache'); $this->setHeader('Cache-Control','public'); } } + function getCacheStart() + { + return $this->dateToTimestamp($_SERVER['HTTP_IF_MODIFIED_SINCE']); + } + + /** + * you can call it like this + * isOlderThan( 2 , 'days' ) + */ + function isOlderThan( $time=0 , $unit='' ) + { + switch (strtolower($unit)) { + case 'week': + case 'weeks': $time = $time*7; + case 'day': + case 'days': $time = $time*24; + case 'hour': + case 'hours': $time = $time*60; + case 'minute': + case 'minutes': $time = $time*60; + case 'second': break; + } + + if ( $time && $this->getCacheStart()+$time < time() ) { + return true; + } + return false; + } + /** * 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 + * 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> @@ -61,7 +90,7 @@ */ function isCached( $condition=true ) { - if( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $condition==true ) { + if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $condition==true ) { return true; } @@ -69,7 +98,7 @@ // 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 ) { + if ( $this->_caching ) { $this->setHeader( 'Last-Modified' ); } return false; @@ -77,10 +106,16 @@ - - function exitIfCached( $condition ) + /** + * 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 + * + * @param boolean will be AND-ed to the 'isCached' call + */ + function exitIfCached( $condition=true ) { - if( $this->isCached( $condition ) ) { + if ( $this->isCached( $condition ) ) { $this->sendHeaders(); $this->sendStatusCode(304); exit;

« previous php.pear.cvs (#8342) next »