PHP Класс HTTP_ConditionalGet, minify

E.g. Content from DB with update time: 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(); }
Автор: Stephen Clay ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Защищенные свойства (Protected)

Свойство Тип Описание
$_etag
$_headers
$_lmTime
$_stripEtag

Открытые методы

Метод Описание
__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

Защищенные методы

Метод Описание
_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

Описание методов

__construct() публичный Метод

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.

_isCacheValid() защищенный Метод

Determine validity of client cache and queue 304 header if valid
protected _isCacheValid ( ) : boolean
Результат boolean

_setEtag() защищенный Метод

protected _setEtag ( string $hash, string $scope )
$hash string
$scope string

_setLastModified() защищенный Метод

protected _setLastModified ( integer $time )
$time integer

check() публичный статический Метод

This is a convenience method for common use of the class
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

getHeaders() публичный Метод

In the case of 304 responses, this array will only contain the response code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified') Otherwise something like: array( 'Cache-Control' => 'max-age=0, public' ,'ETag' => '"foobar"' )
public getHeaders ( ) : array
Результат array

gmtDate() публичный статический Метод

header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
public static gmtDate ( integer $time ) : string
$time integer unix timestamp
Результат string

normalizeEtag() защищенный Метод

protected normalizeEtag ( string $etag ) : string
$etag string
Результат string

resourceMatchedEtag() защищенный Метод

protected resourceMatchedEtag ( ) : boolean
Результат boolean

resourceNotModified() защищенный Метод

protected resourceNotModified ( ) : boolean
Результат boolean

sendHeaders() публичный Метод

Send headers
См. также: getHeaders() Note this doesn't "clear" the headers. Calling sendHeaders() will call header() again (but probably have not effect) and getHeaders() will still return the headers.
public sendHeaders ( ) : null
Результат null

setContentLength() публичный Метод

With most PHP configs, as long as you don't flush() output, this method is not needed and PHP will buffer all output and set Content-Length for you. Otherwise you'll want to call this to let the client know up front.
public setContentLength ( integer $bytes ) : integer
$bytes integer
Результат integer copy of input $bytes

Описание свойств

$_etag защищенное свойство

protected $_etag

$_headers защищенное свойство

protected $_headers

$_lmTime защищенное свойство

protected $_lmTime

$_stripEtag защищенное свойство

protected $_stripEtag

$cacheIsValid публичное свойство

You'll want to check this after instantiating the object. If true, do not send content, just call sendHeaders() if you haven't already.
public bool $cacheIsValid
Результат boolean