-
Notifications
You must be signed in to change notification settings - Fork 25
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
8 changed files
with
68 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,6 +480,7 @@ maintenance and testing these helpers might come handy. | |
import { | ||
createCacheEntry, | ||
assertCacheEntry, | ||
isExpired, | ||
cachified, | ||
} from '@epic-web/cachified'; | ||
|
||
|
@@ -512,6 +513,11 @@ assertCacheEntry(entry); // will throw when entry is not a valid CacheEntry | |
console.log(entry.value); | ||
// > logs "[email protected]" | ||
|
||
/* Manually check if an entry is expired */ | ||
const expired = isExpired(entry.metadata); | ||
console.log(expired); | ||
// > logs true, "stale" or false | ||
|
||
/* Manually remove an entry from cache */ | ||
cache.delete('user-1'); | ||
``` | ||
|
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,47 @@ | ||
import { CacheMetadata, staleWhileRevalidate } from './common'; | ||
|
||
/** | ||
* Check wether a cache entry is expired. | ||
* | ||
* @returns | ||
* - `true` when the cache entry is expired | ||
* - `false` when it's still valid | ||
* - `"stale"` when it's within the stale period | ||
*/ | ||
export function isExpired(metadata: CacheMetadata): boolean | 'stale' { | ||
/* No TTL means the cache is permanent / never expires */ | ||
if (metadata.ttl === null) { | ||
return false; | ||
} | ||
|
||
const validUntil = metadata.createdTime + (metadata.ttl || 0); | ||
const staleUntil = validUntil + (staleWhileRevalidate(metadata) || 0); | ||
const now = Date.now(); | ||
|
||
/* We're still within the ttl period */ | ||
if (now <= validUntil) { | ||
return false; | ||
} | ||
/* We're within the stale period */ | ||
if (now <= staleUntil) { | ||
return 'stale'; | ||
} | ||
|
||
/* Expired */ | ||
return true; | ||
} | ||
|
||
/** | ||
* @deprecated prefer using `isExpired` instead | ||
*/ | ||
export function shouldRefresh( | ||
metadata: CacheMetadata, | ||
): 'now' | 'stale' | false { | ||
const expired = isExpired(metadata); | ||
|
||
if (expired === true) { | ||
return 'now'; | ||
} | ||
|
||
return expired; | ||
} |
This file was deleted.
Oops, something went wrong.
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