Skip to content

Commit 896288b

Browse files
committed
revert(utils): cache usage
1 parent 338edb5 commit 896288b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/utils/cache/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,22 @@ export default {
105105
if (typeof key !== 'string') {
106106
throw new TypeError('Cache key must be a string');
107107
}
108-
const v = await cacheModule.get(key, refresh);
108+
let v = await cacheModule.get(key, refresh);
109109
if (v) {
110+
let parsed;
110111
try {
111-
return JSON.parse(v) as T;
112+
parsed = JSON.parse(v);
112113
} catch {
113-
// Reason: backward compatibility for values stored without JSON.stringify
114-
return v as T;
114+
parsed = null;
115115
}
116+
if (parsed) {
117+
v = parsed;
118+
}
119+
120+
return v as T;
116121
} else {
117122
const value = await getValueFunc();
118-
// Reason: always JSON.stringify to preserve types (e.g. numeric strings
119-
// like Mastodon IDs would lose precision if stored as bare strings and
120-
// later parsed back as numbers by JSON.parse)
121-
cacheModule.set(key, JSON.stringify(value), maxAge);
123+
cacheModule.set(key, value, maxAge);
122124

123125
return value;
124126
}

0 commit comments

Comments
 (0)