Merge pull request #18 from pikasTech/fix/score-cache-reload
Pipelines as Code CI / sub2rank-nc01-2f280e519924cb28b9965e7b8728492870528b91 Success
Pipelines as Code CI / sub2rank-nc01-2f280e519924cb28b9965e7b8728492870528b91 Success
fix: 同步共享评分缓存
This commit is contained in:
@@ -62,7 +62,14 @@ export class AccountScoreService {
|
||||
}
|
||||
|
||||
state(): ScoreSnapshot {
|
||||
return { ...this.snapshot, status: this.inFlight ? "refreshing" : this.snapshot.status };
|
||||
if (!this.inFlight && existsSync(this.cachePath)) this.snapshot = this.readCache();
|
||||
const nextRefreshAt = this.snapshot.nextRefreshAt ? Date.parse(this.snapshot.nextRefreshAt) : Number.NaN;
|
||||
const status = this.inFlight
|
||||
? "refreshing"
|
||||
: this.snapshot.status === "ready" && Number.isFinite(nextRefreshAt) && Date.now() > nextRefreshAt
|
||||
? "stale"
|
||||
: this.snapshot.status;
|
||||
return { ...this.snapshot, status };
|
||||
}
|
||||
|
||||
async refresh(): Promise<ScoreSnapshot> {
|
||||
@@ -119,6 +126,7 @@ export class AccountScoreService {
|
||||
refreshStartedAt: startedAt.toISOString(),
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
this.writeCache(this.snapshot);
|
||||
return this.snapshot;
|
||||
}
|
||||
}
|
||||
@@ -171,7 +179,7 @@ export class AccountScoreService {
|
||||
if (existsSync(this.cachePath)) {
|
||||
try {
|
||||
const cached = record(JSON.parse(readFileSync(this.cachePath, "utf8"))) as ScoreSnapshot | null;
|
||||
if (cached) return { ...cached, status: "stale", accounts: mergeAccountScores(records(cached.accounts)) };
|
||||
if (cached) return { ...cached, accounts: mergeAccountScores(records(cached.accounts)) };
|
||||
} catch {
|
||||
// Invalid cache is replaced by the next successful refresh.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user