Skip to content

Commit a43f53c

Browse files
committed
Update index.js
1 parent a272628 commit a43f53c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ module.exports = class CacheStore {
6363

6464
return this.put(key, value, ttl);
6565
}
66+
async rememberAsync(key, ttl, callback) {
67+
if (this.cacheKeyPrefix) {
68+
key = this.cacheKeyPrefix + key;
69+
}
70+
71+
let localStorageItem = localStorage.getItem(key);
72+
73+
let cachedData = localStorageItem
74+
? JSON.parse(localStorageItem)
75+
: undefined;
76+
77+
// If cached data exists and doesn't expire, or if cached data expires, but still hasn't
78+
if (
79+
cachedData &&
80+
(!cachedData.expiresAt ||
81+
(cachedData.expiresAt &&
82+
cachedData.expiresAt > new Date().toISOString()))
83+
) {
84+
return cachedData.value;
85+
}
86+
87+
this.put(key, undefined, ttl);
88+
89+
let value = await callback();
90+
return this.put(key, value, ttl);
91+
}
6692
rememberForever(key, callback) {
6793
return this.remember(key, null, callback);
6894
}

0 commit comments

Comments
 (0)