Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
branches: [master]
pull_request:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
test:
name: PHP ${{ matrix.php }} / Laravel ${{ matrix.laravel }} (${{ matrix.stability }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.3', '8.4', '8.5']
laravel: ['12', '13']
stability: [prefer-lowest, prefer-stable]
exclude:
- php: '8.2'
laravel: '13'
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: ${{ matrix.php }}
coverage: none
- run: composer update --${{ matrix.stability }} --with "illuminate/support:^${{ matrix.laravel }}.0" --with-all-dependencies --no-interaction --no-progress
- run: composer test

lint:
name: Pint, PHPStan and audit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: '8.5'
coverage: none
- run: composer install --no-interaction --no-progress
- run: vendor/bin/pint --test
- run: composer analyze
- run: composer audit
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Features

```php
Route::get('/example')->middleware([
'cacheResponse:3600,examples'
'response_cache:3600,examples'
])
```

Expand All @@ -31,8 +31,8 @@ This also makes it possible to place the cache check anywhere in your middleware
Requirements
------------

- **PHP 8.0 or later**
- Laravel 7.x or later
- **PHP 8.2 or later**
- Laravel 12.x or 13.x

Installation
------------
Expand All @@ -52,24 +52,29 @@ php artisan vendor:publish --provider "Matchory\ResponseCache\ResponseCacheProvi

Check out the [configuration section](#configuration) to learn about all available options.

Add the middleware to your `app/Http/Kernel.php`:
Register the middleware aliases in your `bootstrap/app.php`:

```php
protected $middlewareGroups = [
'web' => [
// ...
\Matchory\ResponseCache\Http\Middleware\CacheResponse::class,
// ...
],
];

protected $routeMiddleware = [
// ...
'bypass_cache' => \Matchory\ResponseCache\Http\Middleware\BypassCache::class,
->withMiddleware(function (Middleware $middleware): void {
$middleware->alias([
'response_cache' => \Matchory\ResponseCache\Http\Middleware\CacheResponse::class,
'bypass_cache' => \Matchory\ResponseCache\Http\Middleware\BypassCache::class,
]);
})
```

Then add it to your routes, after authentication:

```php
Route::middleware(['auth:sanctum', 'response_cache'])->group(function () {
// ...
];
});
```

> **Important:** The cache must run _after_ authentication. Cached responses are returned before any middleware that follows
> `response_cache` runs, and cache entries are only separated per user if the user is already authenticated when the cache is
> checked. Placing it before your authentication or authorization middleware can serve one user's responses to another.

Configuration
-------------
The config file contains the following settings:
Expand Down Expand Up @@ -138,18 +143,18 @@ use Matchory\ResponseCache\Facades\ResponseCache;

class ExampleObserver {
public function saved(): void {
ResponseCache::clear();
ResponseCache::flush();
}

public function deleted(): void {
// By passing one or more tags, only those items tagged appropriately
// will be cleared
ResponseCache::clear(['example']);
ResponseCache::flush(['example']);
}
}
```

By invoking the `clear` method, the entire store (for the given tags) will be purged. To be more selective, use the `delete` method:
By invoking the `flush` method, the entire store (for the given tags) will be purged. To be more selective, use the `delete` method:

```php
use Matchory\ResponseCache\Facades\ResponseCache;
Expand All @@ -173,7 +178,7 @@ using several approaches:

- By passing them to the middleware:
```php
Route::get('/foo')->middleware(['cache:tag-1,tag-2']);
Route::get('/foo')->middleware(['response_cache:tag-1,tag-2']);
```
- By adding them to the `config/response-cache.php` config file:
```php
Expand All @@ -193,7 +198,7 @@ Take a look at the following example:
```php
Route::get('/api/v{version}/flights/{flight}', function(\App\Flight $flight) {
// ...
})->middleware('cache:api.v{version},flights,flights.v{version},flights.{flight},flights.v{version}.{flight}');
})->middleware('response_cache:api.v{version},flights,flights.v{version},flights.{flight},flights.v{version}.{flight}');
```

Imagine we perform the following request: `/api/v3/flights/505`
Expand Down Expand Up @@ -283,8 +288,7 @@ class MyStrategy extends BaseStrategy
```

### Using the `ResponseCache` middleware manually
Instead of simply adding the caching middleware to all web routes as shown above, you can of course also add it to selected routes manually. In this case, you
also have the possibility to configure the time to leave and add a set of tags per route:
When adding the middleware to routes, you also have the possibility to configure the time to leave and add a set of tags per route:

```php
// Default settings as set in the config file
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
"description": "A package to add automatic response caching to Laravel APIs.",
"type": "library",
"require": {
"php": ">=8.2",
"illuminate/support": "^v10.48.27|^11.40.0|^v12.1.1"
"php": "^8.2",
"illuminate/support": "^12.0|^13.0"
},
"require-dev": {
"illuminate/auth": "^v10.48.27|^11.40.0|^v12.1.1",
"illuminate/cache": "^v10.48.27|^11.40.0|^v12.1.1",
"illuminate/contracts": "^v10.48.27|^v11.40.0|^v12.1.1",
"illuminate/http": "^v10.48.27|^v11.40.0|^v12.1.1",
"illuminate/auth": "^12.0|^13.0",
"illuminate/cache": "^12.0|^13.0",
"illuminate/contracts": "^12.0|^13.0",
"illuminate/http": "^12.0|^13.0",
"jetbrains/phpstorm-attributes": "^1.3",
"laravel/pint": "^1.21",
"orchestra/testbench": "^v10.1.0",
"pestphp/pest": "^v3.7.4",
"pestphp/pest-plugin-laravel": "^3.1.0",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5.3"
"orchestra/testbench": "^10.0|^11.0",
"pestphp/pest": "^3.8|^4.0",
"pestphp/pest-plugin-laravel": "^3.2|^4.0",
"phpstan/phpstan": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
Loading
Loading