list($updateTime, $content) = getDbUpdateAndContent();
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $updateTime
,'isPublic' => true
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
exit();
}
echo $content;
E.g. Shortcut for the above
HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache
echo $content;
E.g. Content from DB with no update time:
$content = getContentFromDB();
$cg = new HTTP_ConditionalGet(array(
'contentHash' => md5($content)
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
exit();
}
echo $content;
E.g. Static content with some static includes:
before content
$cg = new HTTP_ConditionalGet(array(
'lastUpdateTime' => max(
filemtime(__FILE__)
,filemtime('/path/to/header.inc')
,filemtime('/path/to/footer.inc')
)
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
exit();
}
ファイルを表示
Open project: mrclay/minify
Class Usage Examples
Property | Type | Description | |
---|---|---|---|
$cacheIsValid | boolean | You'll want to check this after instantiating the object. If true, do not send content, just call sendHeaders() if you haven't already. |
Property | Type | Description | |
---|---|---|---|
$_etag | |||
$_headers | |||
$_lmTime | |||
$_stripEtag |
Method | Description | |
---|---|---|
__construct ( array $spec ) | ||
check ( integer $lastModifiedTime = null, boolean $isPublic = false, array $options = [] ) | Exit if the client's cache is valid for this resource | |
getHeaders ( ) : array | Get array of output headers to be sent | |
gmtDate ( integer $time ) : string | Get a GMT formatted date for use in HTTP headers | |
sendHeaders ( ) : null | Send headers | |
setContentLength ( integer $bytes ) : integer | Set the Content-Length header in bytes |
Method | Description | |
---|---|---|
_isCacheValid ( ) : boolean | Determine validity of client cache and queue 304 header if valid | |
_setEtag ( string $hash, string $scope ) | ||
_setLastModified ( integer $time ) | ||
normalizeEtag ( string $etag ) : string | ||
resourceMatchedEtag ( ) : boolean | ||
resourceNotModified ( ) : boolean |
public __construct ( array $spec ) | ||
$spec | array | options 'isPublic': (bool) if false, the Cache-Control header will contain "private", allowing only browser caching. (default false) 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers will be sent with content. This is recommended. 'encoding': (string) if set, the header "Vary: Accept-Encoding" will always be sent and a truncated version of the encoding will be appended to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient checking of the client's If-None-Match header, as the encoding portion of the ETag will be stripped before comparison. 'contentHash': (string) if given, only the ETag header can be sent with content (only HTTP1.1 clients can conditionally GET). The given string should be short with no quote characters and always change when the resource changes (recommend md5()). This is not needed/used if lastModifiedTime is given. 'eTag': (string) if given, this will be used as the ETag header rather than values based on lastModifiedTime or contentHash. Also the encoding string will not be appended to the given value as described above. 'invalidate': (bool) if true, the client cache will be considered invalid without testing. Effectively this disables conditional GET. (default false) 'maxAge': (int) if given, this will set the Cache-Control max-age in seconds, and also set the Expires header to the equivalent GMT date. After the max-age period has passed, the browser will again send a conditional GET to revalidate its cache. |
protected _isCacheValid ( ) : boolean | ||
return | boolean |
public static check ( integer $lastModifiedTime = null, boolean $isPublic = false, array $options = [] ) | ||
$lastModifiedTime | integer | if given, both ETag AND Last-Modified headers will be sent with content. This is recommended. |
$isPublic | boolean | (default false) if true, the Cache-Control header will contain "public", allowing proxies to cache the content. Otherwise "private" will be sent, allowing only browser caching. |
$options | array | (default empty) additional options for constructor |
array(
'Cache-Control' => 'max-age=0, public'
,'ETag' => '"foobar"'
)
public getHeaders ( ) : array | ||
return | array |
header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
protected normalizeEtag ( string $etag ) : string | ||
$etag | string | |
return | string |
public setContentLength ( integer $bytes ) : integer | ||
$bytes | integer | |
return | integer | copy of input $bytes |
public bool $cacheIsValid | ||
return | boolean |