Skip to content

A self-hosted media server and file management system

feat: playback caching

aminnausin43dd80948e9f
diff --git a/app/Http/Controllers/Api/V1/PlaybackController.php b/app/Http/Controllers/Api/V1/PlaybackController.php
index 5a057a5e..4535effa 100644
--- a/app/Http/Controllers/Api/V1/PlaybackController.php
+++ b/app/Http/Controllers/Api/V1/PlaybackController.php
@@ -7,6 +7,7 @@ use App\Http\Requests\PlaybackStoreRequest;
use App\Models\Metadata;
use App\Models\Playback;
use App\Traits\HttpResponses;
+use Illuminate\Support\Facades\Cache;
class PlaybackController extends Controller {
use HttpResponses;
@@ -21,38 +22,37 @@ class PlaybackController extends Controller {
* Ordered By Progress 0 -> 100
*/
public function show(int $id) {
- try {
+ return Cache::remember("playback_graph_{$id}", 120, function () use ($id) {
return Playback::where('metadata_id', $id)->oldest('progress')->get();
- } catch (\Throwable $th) {
- return $this->error(null, 'Unable to get playback data. Error: ' . $th->getMessage(), 500);
- }
+ });
}
/**
* Store a newly created resource in storage.
*/
public function store(PlaybackStoreRequest $request) {
- $validated = $request->validated();
- $entries = $validated['entries'] ?? [];
+ $entries = $request->validated()['entries'] ?? [];
try {
- $lastMetadataId = null;
$updates = 0;
+ $lastMetadataId = null;
foreach ($entries as $entry) {
- if ($lastMetadataId !== $entry['metadata_id']) {
- Metadata::findOrFail($entry['metadata_id']);
- $lastMetadataId = $entry['metadata_id'];
+ $metadataId = $entry['metadata_id'];
+
+ if ($lastMetadataId !== $metadataId) {
+ Metadata::findOrFail($metadataId);
+ Cache::forget("playback_graph_{$metadataId}");
+ $lastMetadataId = $metadataId;
}
$existing = Playback::query()
- ->where('metadata_id', $entry['metadata_id'])
+ ->where('metadata_id', $metadataId)
->where('progress', $entry['progress'])
->first();
if ($existing) {
- $entry['count'] = $existing->count + 1;
- $existing->update($entry);
+ $existing->increment('count');
} else {
Playback::create($entry);
}