Skip to content

Commit 855bbf9

Browse files
committed
initial release commit
1 parent 4a56513 commit 855bbf9

17 files changed

+3068
-0
lines changed

.github/workflows/phpunit.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Run PHPUnit Tests
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'README.md'
7+
8+
jobs:
9+
phpunit:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
php-version: [ 8.2, 8.3, 8.4 ]
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-version }}
24+
coverage: none
25+
26+
- name: Install dependencies
27+
run: composer install --no-progress --no-suggest --prefer-dist
28+
29+
- name: Run PHPUnit tests/*
30+
run: vendor/bin/phpunit tests/*

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer.phar

README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# HTTP Cache - `smartondev/httpcache`
2+
3+
**This code is under development and not ready for production use.**
4+
5+
## Installation
6+
7+
```bash
8+
composer require smartondev/httpcache
9+
```
10+
11+
## Usage
12+
13+
```php
14+
use Smartondev\HttpCache\CacheHeaderBuilder;
15+
use Smartondev\HttpCache\ETagHeaderBuilder;
16+
use Smartondev\HttpCache\ETagMatcher;
17+
use SmartonDev\HttpCache\ModifiedMatcher;
18+
19+
// max-age 30 day, private, no-store
20+
$headers = (new CacheHeaderBuilder())
21+
->withMaxAge(hours: 30)
22+
->withPrivate()
23+
->withNoStore()
24+
->toHeaders();
25+
26+
// max-age 60 sec, shared max age 120 sec, stale-while-revalidate 30 sec
27+
$headers = (new CacheHeaderBuilder())
28+
->withMaxAge(60)
29+
->withSharedMaxAge(120)
30+
->withStaleWhileRevalidate(30)
31+
->toHeaders();
32+
33+
// etag
34+
$etagMatcher = (new ETagMatcher())
35+
->withHeaders($requestHeaders);
36+
$etagHeaderBuilder = (new ETagHeaderBuilder())
37+
->withComputedEtag()
38+
if($etagMatcher->matches($etag)->matches()) {
39+
// 304 Not Modified
40+
return response(null, 304);
41+
}
42+
43+
// modified since
44+
$modifiedMatcher = (new ModifiedMatcher())
45+
->withHeaders($requestHeaders);
46+
if($modifiedMatcher->matches($lastModified)->isBeforeModifiedSince()) {
47+
// 304 Not Modified
48+
return response(null, 304);
49+
}
50+
```
51+
52+
...
53+
54+
### Durations
55+
56+
```php
57+
$headers = (new CacheHeaderBuilder())
58+
->withMaxAge(30) // 30 sec
59+
->withMaxAge(seconds: 30) // 30 sec
60+
->withMaxAge(minutes: 30) // 30 min
61+
->withMaxAge(hours: 30) // 30 hours
62+
->withMaxAge(days: 30) // 30 days
63+
->withMaxAge(weeks: 30) // 30 weeks
64+
->withMaxAge(months: 30) // 30 months
65+
->withMaxAge(years: 30) // 30 years
66+
->withMaxAge(days: 10, hours: 5, minutes: 30) // 10 days 5 hours 30 minutes
67+
->toHeaders();
68+
```
69+
70+
...
71+
72+
## Author
73+
74+
- [Márton Somogyi](https://github.com/kamarton)

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "smartondev/httpcache",
3+
"description": "Simple HTTP cache management library",
4+
"type": "library",
5+
"license": "Apache-2.0",
6+
"authors": [
7+
{
8+
"name": "Márton Somogyi",
9+
"email": "kamarton@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": "^8.2"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^11.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"SmartonDev\\HttpCache\\": "src/"
21+
},
22+
"files": [
23+
"src/helpers.php"
24+
]
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"SmartonDev\\HttpCache\\Tests\\": "tests/"
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)