cvs: pear /HTTP_Header Header.php /HTTP_Header/Header Cache.php
| From: | Wolfram Kriesing | Date: | Tue, 09 Dec 2003 18:31:42 +0000 |
| Subject: | cvs: pear /HTTP_Header Header.php /HTTP_Header/Header Cache.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-14735@lists.php.net to get a copy of this message | ||
cain Tue Dec 9 13:31:42 2003 EDT
Modified files:
/pear/HTTP_Header Header.php
/pear/HTTP_Header/Header Cache.php
Log:
+ comments
+ some BC stuff
Index: pear/HTTP_Header/Header.php
diff -u pear/HTTP_Header/Header.php:1.11 pear/HTTP_Header/Header.php:1.12
--- pear/HTTP_Header/Header.php:1.11 Tue Dec 9 13:00:39 2003
+++ pear/HTTP_Header/Header.php Tue Dec 9 13:31:42 2003
@@ -15,7 +15,7 @@
// | Authors: Wolfram Kriesing <wk@visionp.de> |
// | |
// +----------------------------------------------------------------------+//
-// $Id: Header.php,v 1.11 2003/12/09 18:00:39 cain Exp $
+// $Id: Header.php,v 1.12 2003/12/09 18:31:42 cain Exp $
require_once 'HTTP.php';
@@ -145,20 +145,30 @@
}
/**
+ * Send out the header that you set via setHeader(). If the parameter $keys
+ * is given only the headers given in there will be sent.
*
- * @param array the keys that shall be sent, if the array is empty all
- * the headers will be sent (all headers that you would get vie
$this->getHeader())
+ * @param array the keys that shall be sent, if the array is empty all
+ * the headers will be sent (all headers that you would get vie $this->getHeader())
*/
function sendHeaders($keys=array())
{
foreach ($this->_headers as $key=>$value) {
- header("$key: $value");
+ if ($keys && in_array($key,$keys)) {
+ header("$key: $value");
+ } else {
+ header("$key: $value");
+ }
}
}
/**
+ * Send out the given HTTP-Status code.
+ * Use this for example when you want to tell the client this page is
+ * cached, then you would call sendStatusCode(304),
+ * see HTTP_Header_Cache::exitIfCached() for example usage.
*
- *
+ * @param int the status code to be sent, i.e. 404, 304, 200, etc.
*/
function sendStatusCode( $code)
{
@@ -174,7 +184,10 @@
* converts dates like
* Mon, 31 Mar 2003 15:26:34 GMT
* Tue, 15 Nov 1994 12:45:26 GMT
- * into a timestamp, strtotime doesnt do it :-(
+ * into a timestamp, strtotime() doesnt do it :-(
+ *
+ * @param string the data to be converted
+ * @return int the unix-timestamp
*/
function dateToTimestamp($date)
{
Index: pear/HTTP_Header/Header/Cache.php
diff -u pear/HTTP_Header/Header/Cache.php:1.3 pear/HTTP_Header/Header/Cache.php:1.4
--- pear/HTTP_Header/Header/Cache.php:1.3 Tue Mar 4 14:01:36 2003
+++ pear/HTTP_Header/Header/Cache.php Tue Dec 9 13:31:42 2003
@@ -15,15 +15,45 @@
// | Authors: Wolfram Kriesing <wk@visionp.de> |
// | |
// +----------------------------------------------------------------------+//
-// $Id: Cache.php,v 1.3 2003/03/04 19:01:36 cain Exp $
+// $Id: Cache.php,v 1.4 2003/12/09 18:31:42 cain 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>
*
-* @package HTTP_Header
-* @author Wolfram Kriesing <wolfram@kriesing.de>
+* 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>
*/
class HTTP_Header_Cache extends HTTP_Header
{
@@ -38,11 +68,10 @@
* @param integer the number of seconds after which the page expires
* 0 - is never
*/
- function HTTP_Header_Cache( $caching=true )
+ function HTTP_Header_Cache($caching=true)
{
$this->_caching = $caching;
-
- if ( $this->_caching ) {
+ if ($this->_caching) {
$this->setHeader('Pragma','cache');
$this->setHeader('Cache-Control','public');
}
@@ -54,10 +83,30 @@
}
/**
- * you can call it like this
- * isOlderThan( 2 , 'days' )
+ * 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='' )
+ function isOlderThan($time=0, $unit='')
{
switch (strtolower($unit)) {
case 'week':
@@ -70,8 +119,7 @@
case 'minutes': $time = $time*60;
case 'second': break;
}
-
- if ( $time && $this->getCacheStart()+$time < time() ) {
+ if ($time && $this->getCacheStart()+$time<time()) {
return true;
}
return false;
@@ -120,7 +168,7 @@
*/
function exitIfCached( $condition=true )
{
- if ( $this->isCached( $condition ) ) {
+ if ($this->isCached($condition)) {
$this->sendHeaders();
$this->sendStatusCode(304);
exit;