-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4a9e08
commit d25623e
Showing
11 changed files
with
84 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Domain\Videos\Concerns; | ||
|
||
use Domain\Videos\Models\Video; | ||
use Spatie\ResponseCache\Facades\ResponseCache; | ||
|
||
trait InteractsWithCache | ||
{ | ||
public static function bootInteractsWithCache(): void | ||
{ | ||
static::updated(fn (Video $model) => static::clearResponseCache($model)); | ||
|
||
static::deleted(fn (Video $model) => static::clearResponseCache($model)); | ||
} | ||
|
||
public static function clearResponseCache(Video $model): void | ||
{ | ||
ResponseCache::clear(['video-'.$model->getKey()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Support/ResponseCache/Middlewares/CacheModelResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Support\ResponseCache\Middlewares; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Arr; | ||
use Spatie\ResponseCache\Middlewares\CacheResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CacheModelResponse extends CacheResponse | ||
{ | ||
public function handle(Request $request, Closure $next, ...$args): Response | ||
{ | ||
$tags = $this->getTagsByModel($request, $args); | ||
|
||
return parent::handle($request, $next, ...$tags); | ||
} | ||
|
||
protected function getTagsByModel(Request $request, array $args): array | ||
{ | ||
// Get the first parameter from the route to be used for model binding | ||
$parameter = Arr::first($args); | ||
|
||
// Check if the parameter is a model and generate a tag for it | ||
if (($model = $request->route($parameter)) && $model instanceof Model) { | ||
return ["{$parameter}-{$model->getKey()}"]; // e.g. user-1 | ||
} | ||
|
||
return []; | ||
} | ||
} |