Skip to content

Commit

Permalink
Refactor response caching
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Oct 11, 2024
1 parent f4a9e08 commit d25623e
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 28 deletions.
3 changes: 2 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
'abilities' => \Laravel\Sanctum\Http\Middleware\CheckAbilities::class,
'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
'cache' => \Foundation\Http\Middlewares\SetCacheHeaders::class,
'cache_model' => \Support\ResponseCache\Middlewares\CacheModelResponse::class,
'cache_response' => \Spatie\ResponseCache\Middlewares\CacheResponse::class,
'private' => \Foundation\Http\Middlewares\EnsureRequestHasPrivateSubnet::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'response_cache' => \Spatie\ResponseCache\Middlewares\CacheResponse::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'subscribed' => \App\Api\Users\Middlewares\EnsureUserHasSubscription::class,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"laravel/tinker": "^2.10.0",
"league/flysystem-aws-s3-v3": "^3.29",
"league/flysystem-read-only": "^3.28",
"livewire/livewire": "^3.5.9",
"livewire/livewire": "^3.5.10",
"meilisearch/meilisearch-php": "^1.10.1",
"php-ffmpeg/php-ffmpeg": "^1.2",
"pusher/pusher-php-server": "^7.2.4",
Expand Down
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/App/Api/Authentication/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HomeController extends Controller implements HasMiddleware
public static function middleware(): array
{
return [
new Middleware('response_cache:600'),
new Middleware('cache_response:600'),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/App/Api/Users/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function middleware(): array
new Middleware('throttle:none'),
new Middleware('auth:sanctum'),
new Middleware('subscribed'),
new Middleware('response_cache:600,user-'.auth()->id()),
new Middleware('cache_response:600,user-'.auth()->id()),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/App/Api/Videos/Controllers/ManifestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function middleware(): array
return [
new Middleware('private'),
new Middleware('throttle:none'),
new Middleware('response_cache:600'),
new Middleware('cache_model:600,video'),
];
}

Expand Down
1 change: 0 additions & 1 deletion src/App/Web/Library/Forms/QueryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class QueryForm extends Form
#[Validate('nullable|string|in:untagged,new')]
public string $type = '';


protected function beforeValidate(): void
{
app(Video::class)->forgetRandomSeed('videos-recommended');
Expand Down
6 changes: 3 additions & 3 deletions src/Domain/Users/Concerns/InteractsWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ trait InteractsWithCache
{
public static function bootInteractsWithCache(): void
{
static::updated(fn (User $model) => static::forgetResponseCache($model));
static::updated(fn (User $model) => static::clearResponseCache($model));

static::deleted(fn (User $model) => static::forgetResponseCache($model));
static::deleted(fn (User $model) => static::clearResponseCache($model));
}

public static function forgetResponseCache(User $model): void
public static function clearResponseCache(User $model): void
{
ResponseCache::clear(['user-'.$model->getKey()]);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Domain/Videos/Concerns/InteractsWithCache.php
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()]);
}
}
2 changes: 2 additions & 0 deletions src/Domain/Videos/Models/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Domain\Tags\Concerns\HasTags;
use Domain\Users\Concerns\InteractsWithUser;
use Domain\Videos\Collections\VideoCollection;
use Domain\Videos\Concerns\InteractsWithCache;
use Domain\Videos\Concerns\InteractsWithPlaylists;
use Domain\Videos\Concerns\InteractsWithVod;
use Domain\Videos\QueryBuilders\VideoQueryBuilder;
Expand All @@ -34,6 +35,7 @@ class Video extends Model implements HasMedia
use HasTags;
use HasTranslations;
use InteractsWithActivity;
use InteractsWithCache;
use InteractsWithMedia;
use InteractsWithPlaylists;
use InteractsWithRandomSeed;
Expand Down
33 changes: 33 additions & 0 deletions src/Support/ResponseCache/Middlewares/CacheModelResponse.php
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 [];
}
}

0 comments on commit d25623e

Please sign in to comment.