-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats-service.js
More file actions
73 lines (63 loc) · 1.77 KB
/
Copy pathstats-service.js
File metadata and controls
73 lines (63 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { aggregateStats } from './aggregator.js';
import { loadPricingConfig, savePricingConfig, validatePricingConfig } from './pricing.js';
const CACHE_TTL = 30_000;
let cachedStats = null;
let cacheTime = 0;
let cachedPricingUpdated = '';
/**
* 获取(必要时重算)聚合统计。pricing.updated 变化或超时都会触发重算。
* @param {{ forceFresh?: boolean }} [options]
*/
export async function getStats({ forceFresh = false } = {}) {
const now = Date.now();
const pricingConfig = await loadPricingConfig();
const currentUpdated = pricingConfig.updated || '';
const expired = now - cacheTime > CACHE_TTL;
const pricingChanged = currentUpdated !== cachedPricingUpdated;
if (forceFresh || !cachedStats || expired || pricingChanged) {
cachedStats = await aggregateStats(pricingConfig);
cachedStats.pricingUpdated = currentUpdated;
cachedStats.pricingVersion = pricingConfig.version;
cacheTime = now;
cachedPricingUpdated = currentUpdated;
}
return cachedStats;
}
/**
* 使统计缓存失效。
*/
export function invalidateStatsCache() {
cachedStats = null;
cacheTime = 0;
cachedPricingUpdated = '';
}
/**
* 读取价格配置。
*/
export async function getPricingConfig() {
return loadPricingConfig();
}
/**
* 更新价格配置并失效缓存。
* @param {object} config
*/
export async function updatePricingConfig(config) {
validatePricingConfig(config);
await savePricingConfig(config);
invalidateStatsCache();
return {
ok: true,
updated: config.updated,
};
}
/**
* 强制刷新统计缓存。
*/
export async function refreshStatsCache() {
const data = await getStats({ forceFresh: true });
return {
ok: true,
generatedAt: data.generatedAt,
pricingVersion: data.pricingVersion,
};
}