File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,32 @@ module.exports = class CacheStore {
63
63
64
64
return this . put ( key , value , ttl ) ;
65
65
}
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
+ }
66
92
rememberForever ( key , callback ) {
67
93
return this . remember ( key , null , callback ) ;
68
94
}
You can’t perform that action at this time.
0 commit comments