This package helps you to build HTTP cache headers like Cache-Control, ETag and matchers like If-None-Match,
If-Modified-Since. It is useful for building HTTP cache headers and matchers in your application.
composer require smartondev/httpcacheuse SmartonDev\HttpCache\Builders\CacheHeaderBuilder;
// max-age 1 hour, private, no-store
$headers = (new CacheHeaderBuilder())
->maxAge(hours: 1)
->private()
->noStore()
->toHeaders();
// max-age 60 sec, shared max age 120 sec, stale-while-revalidate 30 sec
$headers = (new CacheHeaderBuilder())
->maxAge(60)
->sharedMaxAge(120)
->staleWhileRevalidate(30)
->toHeaders();$noCacheHeaders = (new CacheHeaderBuilder())
->noCache()
->toHeaders();$builder = (new CacheHeaderBuilder())
->maxAge(30) // 30 sec
->maxAge(seconds: 30) // 30 sec
->maxAge(minutes: 30) // 30 min
->maxAge(hours: 30) // 30 hours
->maxAge(days: 30) // 30 days
->maxAge(weeks: 30) // 30 weeks
->maxAge(months: 30) // 30 months
->maxAge(years: 30) // 30 years
->maxAge(days: 10, hours: 5, minutes: 30) // 10 days 5 hours 30 minutesuse SmartonDev\HttpCache\Matchers\ETagMatcher;
// ETag check
$etagMatcher = (new ETagMatcher())
->headers($requestHeaders);
$activeEtag = '1234';
if($etagMatcher->matches($activeEtag)->matches()) {
// 304 Not Modified
return response(null, 304);
}use SmartonDev\HttpCache\Matchers\ModifiedMatcher;
// modified since
$modifiedMatcher = (new ModifiedMatcher())
->headers($requestHeaders);
if($modifiedMatcher->matches($lastModified)->matchesModifiedAt()) {
// 304 Not Modified
return response(null, 304);
}withprefixed methods are immutable, eg.withMaxAge(). Methods withoutwithprefix are mutable, eg.maxAge().withoutprefixed methods are immutable, eg.withoutMaxAge(). Methods withresetprefix are mutable, eg.resetMaxAge().
$builderA = new CacheHeaderBuilder();
// mutable
$builderA->maxAge(30)
->resetMaxAge();
// immutable
$builderB = $builderA->withMaxAge(60);
$builderC = $builderB->withoutMaxAge();- CacheHeaderBuilder: building cache headers like
Cache-Control - ETagHeaderBuilder: building ETag header
- ETagMatcher: matching ETag headers like
If-Match,If-None-Match - ModifiedMatcher: matching modified headers like
If-Modified-Since,If-Unmodified-Since
Please see CONTRIBUTING for details.
This project is open-sourced software licensed under.