-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cache): Utils to calculate soft and hard TTL (#33844)
- Loading branch information
Showing
5 changed files
with
69 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
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,43 @@ | ||
import is from '@sindresorhus/is'; | ||
import { GlobalConfig } from '../../../config/global'; | ||
import type { PackageCacheNamespace } from './types'; | ||
|
||
export function getTtlOverride( | ||
namespace: PackageCacheNamespace, | ||
): number | undefined { | ||
const ttl = GlobalConfig.get('cacheTtlOverride', {})[namespace]; | ||
if (is.number(ttl)) { | ||
return ttl; | ||
} | ||
return undefined; | ||
} | ||
|
||
export interface TTLValues { | ||
/** TTL for serving cached value without hitting the server */ | ||
softTtlMinutes: number; | ||
|
||
/** TTL for serving stale cache when upstream responds with errors */ | ||
hardTtlMinutes: number; | ||
} | ||
|
||
/** | ||
* Apply user-configured overrides and return the final values for soft/hard TTL. | ||
* | ||
* @param namespace Cache namespace | ||
* @param ttlMinutes TTL value configured in Renovate codebase | ||
* @returns | ||
*/ | ||
export function resolveTtlValues( | ||
namespace: PackageCacheNamespace, | ||
ttlMinutes: number, | ||
): TTLValues { | ||
const softTtlMinutes = getTtlOverride(namespace) ?? ttlMinutes; | ||
|
||
const cacheHardTtlMinutes = GlobalConfig.get( | ||
'cacheHardTtlMinutes', | ||
7 * 24 * 60, | ||
); | ||
const hardTtlMinutes = Math.max(softTtlMinutes, cacheHardTtlMinutes); | ||
|
||
return { softTtlMinutes, hardTtlMinutes }; | ||
} |
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