Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurYdalgo committed Aug 25, 2023
1 parent a272628 commit a43f53c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ module.exports = class CacheStore {

return this.put(key, value, ttl);
}
async rememberAsync(key, ttl, callback) {
if (this.cacheKeyPrefix) {
key = this.cacheKeyPrefix + key;
}

let localStorageItem = localStorage.getItem(key);

let cachedData = localStorageItem
? JSON.parse(localStorageItem)
: undefined;

// If cached data exists and doesn't expire, or if cached data expires, but still hasn't
if (
cachedData &&
(!cachedData.expiresAt ||
(cachedData.expiresAt &&
cachedData.expiresAt > new Date().toISOString()))
) {
return cachedData.value;
}

this.put(key, undefined, ttl);

let value = await callback();
return this.put(key, value, ttl);
}
rememberForever(key, callback) {
return this.remember(key, null, callback);
}
Expand Down

0 comments on commit a43f53c

Please sign in to comment.