Skip to content

Commit 09bfb47

Browse files
committed
refactor: centralize cache TTL constants for Cloudflare IPs and RDAP bootstrap data
1 parent 7a8343b commit 09bfb47

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

lib/cloudflare.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import "server-only";
33
import * as ipaddr from "ipaddr.js";
44
import { cache } from "react";
55
import { acquireLockOrWaitForResult } from "@/lib/cache";
6-
import { CLOUDFLARE_IPS_URL } from "@/lib/constants";
6+
import {
7+
CLOUDFLARE_IPS_CACHE_TTL_SECONDS,
8+
CLOUDFLARE_IPS_URL,
9+
} from "@/lib/constants";
710
import { ipV4InCidr, ipV6InCidr } from "@/lib/ip";
811
import { redis } from "@/lib/redis";
912

@@ -14,7 +17,6 @@ export interface CloudflareIpRanges {
1417

1518
const CACHE_KEY = "cloudflare:ip-ranges";
1619
const LOCK_KEY = "cloudflare:ip-ranges:lock";
17-
const CACHE_TTL_SECONDS = 604800; // 1 week
1820

1921
let lastLoadedIpv4Parsed: Array<[ipaddr.IPv4, number]> | undefined;
2022
let lastLoadedIpv6Parsed: Array<[ipaddr.IPv6, number]> | undefined;
@@ -101,7 +103,7 @@ const getCloudflareIpRanges = cache(async (): Promise<CloudflareIpRanges> => {
101103
try {
102104
ranges = await fetchCloudflareIpRanges();
103105
await redis.set(CACHE_KEY, ranges, {
104-
ex: CACHE_TTL_SECONDS,
106+
ex: CLOUDFLARE_IPS_CACHE_TTL_SECONDS,
105107
});
106108
parseAndCacheRanges(ranges);
107109
console.info("[cloudflare-ips] IP ranges fetched (not cached)");

lib/constants.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,35 @@ export const USER_AGENT =
88

99
export const REPOSITORY_SLUG = "jakejarvis/domainstack.io";
1010

11+
// Time constants (in seconds)
12+
const ONE_MINUTE = 60;
13+
const ONE_HOUR = ONE_MINUTE * 60;
14+
const ONE_DAY = ONE_HOUR * 24;
15+
const ONE_WEEK = ONE_DAY * 7;
16+
1117
/**
1218
* RDAP Bootstrap Registry URL from IANA.
1319
* This JSON file maps TLDs to their authoritative RDAP servers.
1420
* @see https://datatracker.ietf.org/doc/html/rfc7484
1521
*/
1622
export const RDAP_BOOTSTRAP_URL = "https://data.iana.org/rdap/dns.json";
23+
/**
24+
* RDAP Bootstrap cache TTL in seconds.
25+
* The bootstrap registry changes very infrequently, so we cache it for 1 week.
26+
*/
27+
export const RDAP_BOOTSTRAP_CACHE_TTL_SECONDS = ONE_WEEK;
1728

1829
/**
1930
* Cloudflare IP Ranges URL.
2031
* This JSON file contains the IP ranges for Cloudflare's network.
2132
* @see https://developers.cloudflare.com/api/resources/ips/methods/list/
2233
*/
2334
export const CLOUDFLARE_IPS_URL = "https://api.cloudflare.com/client/v4/ips";
24-
25-
// Time constants (in seconds)
26-
const ONE_HOUR = 60 * 60;
27-
const ONE_DAY = 24 * ONE_HOUR;
28-
const ONE_WEEK = 7 * ONE_DAY;
35+
/**
36+
* Cloudflare IP Ranges cache TTL in seconds.
37+
* The IP ranges change infrequently, so we cache it for 1 week.
38+
*/
39+
export const CLOUDFLARE_IPS_CACHE_TTL_SECONDS = 604800; // 1 week
2940

3041
// ===== Blob Storage Cache TTLs =====
3142
// How long to cache uploaded assets (favicons, screenshots, social images)

lib/rdap-bootstrap.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import "server-only";
33
import type { BootstrapData } from "rdapper";
44
import { cache } from "react";
55
import { acquireLockOrWaitForResult } from "@/lib/cache";
6-
import { RDAP_BOOTSTRAP_URL } from "@/lib/constants";
6+
import {
7+
RDAP_BOOTSTRAP_CACHE_TTL_SECONDS,
8+
RDAP_BOOTSTRAP_URL,
9+
} from "@/lib/constants";
710
import { redis } from "@/lib/redis";
811

912
const CACHE_KEY = "rdap:bootstrap";
1013
const LOCK_KEY = "rdap:bootstrap:lock";
11-
const CACHE_TTL_SECONDS = 604800; // 1 week (bootstrap changes very infrequently)
1214

1315
/**
1416
* Fetch RDAP bootstrap data with Redis caching.
@@ -48,7 +50,7 @@ export const getRdapBootstrapData = cache(async (): Promise<BootstrapData> => {
4850

4951
bootstrap = await res.json();
5052
await redis.set(CACHE_KEY, bootstrap, {
51-
ex: CACHE_TTL_SECONDS,
53+
ex: RDAP_BOOTSTRAP_CACHE_TTL_SECONDS,
5254
});
5355
console.info("[rdap-bootstrap] Bootstrap data fetched (not cached)");
5456
} catch (err) {

0 commit comments

Comments
 (0)