-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
97 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const MILLISECONDS_IN_DAY = 86400000; | ||
|
||
const refreshCache = async (cacheKey, expirationTime, fetchFunction) => { | ||
const result = await fetchFunction(); | ||
|
||
const jsonValue = JSON.stringify({ | ||
data: result, | ||
expires_at: Date.now() + expirationTime, | ||
}); | ||
await localStorage.setItem(cacheKey, jsonValue); | ||
|
||
return result; | ||
}; | ||
|
||
export const fetchFromCache = async ( | ||
cacheKey, | ||
fetchFunction, | ||
expirationTime = MILLISECONDS_IN_DAY, | ||
useCache = true | ||
) => { | ||
// if no using cache | ||
if (!useCache) { | ||
return refreshCache(cacheKey, expirationTime, fetchFunction); | ||
} | ||
|
||
const jsonValue = await localStorage.getItem(cacheKey); | ||
const parsedJsonValue = jsonValue != null ? JSON.parse(jsonValue) : null; | ||
|
||
// if not expired -> return data from cache | ||
if (parsedJsonValue && parsedJsonValue.expires_at > Date.now()) { | ||
return parsedJsonValue.data; | ||
} | ||
|
||
// if cache is expired or no value in cache | ||
return refreshCache(cacheKey, expirationTime, fetchFunction); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters