PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTTP » HTTP_Download » Bug #4754

[PATCH] Add cache length to API

Details

Request #4754[PATCH] Add cache length to API
Submitted2005-07-05 22:20 UTC
Fromaron-phppear at sightspeed dot com
Assignedmike
StatusClosed
PackageHTTP_Download
PHP Version5.0.4
Roadmaps(Not assigned)

Comments

[2005-07-05 22:20 UTC] aron-phppear at sightspeed dot com

Description:
------------
This patch allows the ability to set via the Class api how long a file should be cached for instead of just forcing the setting to 0 which forces a revalidate everytime. The default time is 3600 seconds which is one hour.

Reproduce code:
---------------
--- Download.php.orig 2005-06-06 18:11:10.000000000 -0700
+++ Download.php 2005-07-05 15:10:21.000000000 -0700
@@ -140,6 +140,22 @@
* @var bool
*/
var $cache = true;
+
+ /**
+ * Length of time (in seconds) the cache is valid for. Only valid if caching is enabled.
+ *
+ * @access protected
+ * @var int
+ */
+ var $cacheTime = 3600;
+
+ /**
+ * If we are allowing caching, should a proxy be allowed to cache the file
+ *
+ * @access protected
+ * @var string
+ */
+ var $cachePrivacy = 'public';

/**
* Size of download
@@ -166,7 +182,7 @@
var $headers = array(
'Content-Type' => 'application/x-octetstream',
'Pragma' => 'cache',
- 'Cache-Control' => 'public, must-revalidate, max-age=0',
+ 'Cache-Control' => 'public, must-revalidate, max-age=3600',
'Accept-Ranges' => 'bytes',
'X-Sent-By' => 'PEAR::HTTP::Download'
);
@@ -401,6 +417,22 @@
{
$this->cache = (bool) $cache;
}
+
+ /**
+ * Set the length of time (in seconds) that the cache should be valid
+ * for. Ex: 3600 is one hour
+ *
+ * If set to 0, browser should reverify at each use.
+ *
+ * @access public
+ * @return void
+ * @param int $cache whether to allow caching
+ */
+ function setCacheValidTime($cacheTime = 0)
+ {
+ $this->cacheTime = (int) $cacheTime;
+ $this->headers['Cache-Control'] = $this->cachePrivacy .', must-revalidate, max-age=' . $this->cacheTime;
+ }

/**
* Whether to allow proxies to cache
@@ -418,8 +450,9 @@
{
case 'private':
case 'public':
+ $this->cachePrivacy = $cache;
$this->headers['Cache-Control'] =
- $cache .', must-revalidate, max-age=0';
+ $this->cachePrivacy .', must-revalidate, max-age=' . $this->cacheTime;
return true;
break;
}