-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Electron vite built : first working (building) version
- Loading branch information
Showing
9 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Binary file not shown.
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,38 @@ | ||
import fetch from "node-fetch"; | ||
|
||
const cache = {}; | ||
|
||
/** | ||
* Fetches data from a URL with caching capability. | ||
* If cached data is available and not expired, it returns the cached data. | ||
* Otherwise, it fetches from the network, caches the new data, and returns it. | ||
* | ||
* @param {string} url - The URL to fetch the data from. | ||
* @param {number} ttl - Time to live for the cache in seconds. | ||
* @returns {Promise<any>} The fetched data. | ||
*/ | ||
export async function fetchCacheData(url, ttl) { | ||
const now = Date.now(); | ||
const item = cache[url]; | ||
|
||
// Check if item exists and is still valid | ||
if (item && item.expires > now) { | ||
return item.data; | ||
} | ||
|
||
// If the item is expired or doesn't exist, fetch new data | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
throw new Error( | ||
`HTTP error while fetching data. Status: ${res.status} - ${res.statusText}` | ||
); | ||
} | ||
|
||
const data = await res.json(); | ||
const expires = now + ttl * 1000; // Calculate the expiration time | ||
|
||
// Cache the new data with expiration time | ||
cache[url] = { data, expires }; | ||
|
||
return data; | ||
} |
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