Files
gitbook/src/lib/cache/utils.ts
T
Samy Pessé 221d85cc4d Optimize caching of revisions/documents (immutable entries) in memory and cloudflare cache (#252)
* 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>
2024-03-13 12:54:22 +01:00

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;
}