Skip to content

Commit

Permalink
avoid querying Activity table for recent logins (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Oct 7, 2023
1 parent 8fef4e2 commit da3dea2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
23 changes: 7 additions & 16 deletions app/Helpers/database/user-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,14 @@ function postActivity(string|User $userIn, int $type, ?int $data = null, ?int $d
break;

case ActivityType::Login:
$lastLoginActivity = getMostRecentActivity($user->User, $type);
if ($lastLoginActivity) {
$nowTimestamp = time();
$lastLoginTimestamp = strtotime($lastLoginActivity['timestamp']);
$diff = $nowTimestamp - $lastLoginTimestamp;

/*
* record login activity only every 6 hours
*/
if ($diff < 60 * 60 * 6) {
/*
* new login activity from $user, duplicate of recent login " . ($diff/60) . " mins ago,
* ignoring!
*/
return true;
}
/* only record login activity every six hours */
$cacheKey = CacheKey::buildUserLastLoginCacheKey($user->User);
$lastLogin = Cache::get($cacheKey);
if ($lastLogin && $lastLogin > Carbon::now()->subHours(6)) {
/* ignore event, login recorded recently */
return true;
}
Cache::put($cacheKey, Carbon::now(), Carbon::now()->addHours(6));
break;

case ActivityType::StartedPlaying:
Expand Down
5 changes: 5 additions & 0 deletions app/Support/Cache/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static function buildGameCardDataCacheKey(int $gameId): string
return self::buildNormalizedCacheKey("game", $gameId, "card-data");
}

public static function buildUserLastLoginCacheKey(string $username): string
{
return self::buildNormalizedUserCacheKey($username, "last-login");
}

public static function buildUserCompletedGamesCacheKey(string $username): string
{
return self::buildNormalizedUserCacheKey($username, "completed-games");
Expand Down

0 comments on commit da3dea2

Please sign in to comment.