perf(user): 关注结算异步化并去除冗余勋章评估

This commit is contained in:
2026-08-23 15:44:56 +08:00
parent 2c1f08deb1
commit 42288bb0b1
4 changed files with 39 additions and 34 deletions
+3 -3
View File
@@ -59,12 +59,12 @@ class BadgeAutoAward {
}
public function on_follow(int $follower_id, int $following_id): void {
unset($follower_id);
// No medal metric depends on how many users someone follows, so only
// the followed user (whose follower count changed) can earn here.
if ($following_id > 0) {
$this->evaluate($following_id);
}
if ($follower_id > 0) {
$this->evaluate($follower_id);
}
}
public function on_checkin(int $user_id): void {
+15 -1
View File
@@ -47,10 +47,24 @@ class Follow {
if ($result === false) {
return false;
}
do_action('aksrt_user_followed', $follower_id, $following_id);
self::defer_action('aksrt_user_followed', $follower_id, $following_id);
return true;
}
/**
* 在响应送达客户端之后再触发结算钩子(积分/勋章/等级),
* 避免重度订阅者阻塞关注接口。非 PHP-FPM 环境退化为
* shutdown 时机同步执行,行为与直接 do_action 等价。
*/
private static function defer_action(string $hook, int ...$args): void {
register_shutdown_function(static function () use ($hook, $args): void {
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
do_action($hook, ...$args);
});
}
/**
* 取消关注
*/
-8
View File
@@ -31,7 +31,6 @@ class Metrics {
add_action('aksrt_post_liked', [$this, 'on_post_liked'], 20, 3);
add_action('aksrt_comment_liked', [$this, 'on_comment_liked'], 20, 3);
add_action('aksrt_post_favorited', [$this, 'on_post_favorited'], 20, 3);
add_action('aksrt_user_followed', [$this, 'on_user_followed'], 20, 2);
add_action('aksrt_post_viewed', [$this, 'on_post_viewed'], 20, 2);
add_action('aksrt_user_signed_in', [$this, 'on_user_signed_in'], 20, 1);
@@ -90,13 +89,6 @@ class Metrics {
// Favorite is free-points / level only; no dedicated medal get_type.
}
public function on_user_followed(int $follower_id, int $following_id): void {
unset($follower_id);
if ($following_id > 0) {
self::reevaluate_medals($following_id);
}
}
public function on_post_viewed(int $post_id, int $viewer_id = 0): void {
unset($viewer_id);
$post = get_post($post_id);