Skip to content

Commit

Permalink
feat: Purge caches on uploading results
Browse files Browse the repository at this point in the history
Part of #18
  • Loading branch information
yukidaruma committed Aug 13, 2020
1 parent f7973c7 commit 242cae4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
25 changes: 25 additions & 0 deletions app/Helpers/CacheHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Cache;

class CacheHelper
{
public static function purgePlayerCaches(string $playerId)
{
$purgedCacheCount = 0;

$cacheKeyPrefixes = [
'players.metadata',
'players.weapons',
];

foreach ($cacheKeyPrefixes as $cacheKeyPrefix) {
$key = "$cacheKeyPrefix.$playerId";
$purgedCacheCount += Cache::forget($key) ? 1 : 0;
}

\Log::debug("Purged $purgedCacheCount cache(s) for $playerId.");
}
}
18 changes: 16 additions & 2 deletions app/Http/Controllers/SalmonResultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Constants\SalmonStatsConst;
use App\Helpers\CacheHelper;
use App\Helpers\Helper;
use App\Helpers\SalmonResultQueryHelper;
use App\SalmonPlayerResult;
Expand Down Expand Up @@ -45,6 +46,7 @@ public function create()
protected function createRecords($job, $user, $uploaderPlayerId)
{
return function () use ($job, $user, $uploaderPlayerId) {
$affectedPlayerIds = [];
$playerJobId = $job['job_id'] ?: null;
$playerResults = array_merge([$job['my_result']], $job['other_results']);
usort($playerResults, function ($a, $b) { return $a['pid'] > $b['pid'] ? 1 : -1; });
Expand Down Expand Up @@ -147,6 +149,8 @@ function ($sum, $wave) { return $sum + $wave['ikura_num']; },
}

foreach ($playerResults as $playerResult) {
$affectedPlayerIds[] = $playerResult['pid'];

$bossKillCounts = Helper::mapCount($playerResult['boss_kill_counts']);
$salmonPlayerResult = [
'salmon_id' => $salmonResult->id,
Expand Down Expand Up @@ -230,6 +234,7 @@ function ($sum, $wave) { return $sum + $wave['ikura_num']; },
'created' => true,
'job_id' => $playerJobId,
'salmon_id' => $salmonResult->id,
'affected_players' => $affectedPlayerIds,
];
};
}
Expand Down Expand Up @@ -270,15 +275,24 @@ public function store(Request $request)

try {
$results[] = DB::transaction($this->createRecords($job, $user, $uploaderPlayerId));
}
catch (\Exception $e) {
} catch (\Exception $e) {
Log::error($e);
abort(500, "Unhandled Exception: {$e->getMessage()}");
}
}

\Auth::user()->touch();

// Purge cache for affected players
$affectedPlayerIds = collect($results)
->filter(fn ($result) => array_key_exists('affected_players', $result))
->map(fn ($result) => $result['affected_players'])
->flatten()
->unique();
foreach ($affectedPlayerIds as $affectedPlayerId) {
CacheHelper::purgePlayerCaches($affectedPlayerId);
}

if ($request->query('mode') === 'object') {
return response()->json([
'upload_results' => $results,
Expand Down

0 comments on commit 242cae4

Please sign in to comment.