1
0
mirror of https://github.com/flarum/core.git synced 2025-10-27 05:31:29 +01:00

perf: only update last time when current value outdated (#3230)

* perf: only update last seen time when current > 120s ago

* perf: only update `last_activity_at` every 2 mins

* docs: add comment

* fix: add missing param

* test: add tests

* tests: attempt tests fix

* fix(tests): call `$this->app()`

* chore: extract hard-coded values out to private consts

* chore: increase diff

* Apply suggestions from code review
This commit is contained in:
David Wheatley
2021-12-28 00:39:42 +01:00
committed by GitHub
parent 64dab138c4
commit 2e94e31bb6
3 changed files with 80 additions and 9 deletions

View File

@@ -63,6 +63,12 @@ class AccessToken extends AbstractModel
*/
protected static $lifetime = 0;
/**
* Difference from the current `last_activity_at` attribute value before `updateLastSeen()`
* will update the attribute on the DB. Measured in seconds.
*/
private const LAST_ACTIVITY_UPDATE_DIFF = 90;
/**
* Generate an access token for the specified user.
*
@@ -95,7 +101,11 @@ class AccessToken extends AbstractModel
*/
public function touch(ServerRequestInterface $request = null)
{
$this->last_activity_at = Carbon::now();
$now = Carbon::now();
if ($this->last_activity_at === null || $this->last_activity_at->diffInSeconds($now) > AccessToken::LAST_ACTIVITY_UPDATE_DIFF) {
$this->last_activity_at = $now;
}
if ($request) {
$this->last_ip_address = $request->getAttribute('ipAddress');