mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 10:33:22 +00:00
221d85cc4d
* Better handle immutable cache entries in memory/cf-cache * Format * Update src/lib/cache/utils.ts Co-authored-by: Steven H <shne24@gmail.com> --------- Co-authored-by: Steven H <shne24@gmail.com>
22 lines
554 B
TypeScript
22 lines
554 B
TypeScript
import { CacheEntryMeta } from './types';
|
|
|
|
/**
|
|
* Get the max-age in seconds for a cache entry.
|
|
*/
|
|
export function getCacheMaxAge(meta: CacheEntryMeta, min?: number, max?: number): number {
|
|
let maxAge = Math.max(min ?? 0, Math.round((meta.expiresAt - Date.now()) / 1000));
|
|
|
|
if (max) {
|
|
maxAge = Math.min(max, maxAge);
|
|
}
|
|
|
|
return maxAge;
|
|
}
|
|
|
|
/**
|
|
* Return true if a cache entry can be considered immutable.
|
|
*/
|
|
export function isCacheEntryImmutable(meta: CacheEntryMeta): boolean {
|
|
return !meta.tags || meta.tags.length === 0;
|
|
}
|