-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
85 lines (75 loc) · 1.95 KB
/
cache.js
File metadata and controls
85 lines (75 loc) · 1.95 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
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Redis cache layer for upstream API calls.
*
* Uses its own DB (5) to avoid collision with trading bot's Redis usage (DB 0).
* Short TTLs: prices/funding/gas etc change fast, so 5-30s is the sweet spot.
*
* Usage:
* const data = await cached('prices', 10, async () => fetchFromMEXC());
*/
import Redis from 'ioredis';
const redis = new Redis({
host: '127.0.0.1',
port: 6379,
db: 5, // isolated from trading bot (which uses 0)
lazyConnect: true,
maxRetriesPerRequest: 2,
retryStrategy: (times) => Math.min(times * 100, 2000),
});
redis.on('error', (e) => {
// Don't spam; log once per minute
if (!redis._lastErrorLog || Date.now() - redis._lastErrorLog > 60_000) {
console.error('[cache] Redis error:', e.message);
redis._lastErrorLog = Date.now();
}
});
let hits = 0;
let misses = 0;
let errors = 0;
/**
* Cache wrapper. If `key` is in Redis, return it. Otherwise call `fetcher`,
* store the result with `ttl` seconds, and return it.
*
* Never throws — falls back to direct fetcher call if Redis is unavailable.
*/
export async function cached(key, ttl, fetcher) {
const fullKey = `agentdata:${key}`;
try {
const raw = await redis.get(fullKey);
if (raw !== null) {
hits++;
return JSON.parse(raw);
}
} catch (e) {
errors++;
// Fall through to direct fetch
}
misses++;
const value = await fetcher();
// Write-through (fire-and-forget)
try {
await redis.set(fullKey, JSON.stringify(value), 'EX', ttl);
} catch (e) {
errors++;
}
return value;
}
export function getCacheStats() {
const total = hits + misses;
return {
hits,
misses,
errors,
hitRate: total > 0 ? (hits / total * 100).toFixed(1) + '%' : 'n/a',
total,
};
}
export async function flushCache() {
try {
const keys = await redis.keys('agentdata:*');
if (keys.length) await redis.del(...keys);
return { cleared: keys.length };
} catch (e) {
return { error: e.message };
}
}