Skip to content

Commit 5d57ca7

Browse files
committed
feat: add string caching optimization to fetchf function and benchmark test
1 parent 563fb9a commit 5d57ca7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/request-handler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ export async function fetchf<
6363
RequestBody
6464
> | null = null,
6565
): Promise<FetchResponse<ResponseData, RequestBody, QueryParams, PathParams>> {
66+
// Ultra-fast early cache check if cacheKey is provided as a string
67+
// For workloads dominated by repeated requests, this string caching optimization
68+
// can potentially support millions of requests per second with minimal CPU overhead
69+
if (reqConfig && typeof reqConfig.cacheKey === 'string') {
70+
const cached = getCachedResponse<
71+
ResponseData,
72+
RequestBody,
73+
QueryParams,
74+
PathParams
75+
>(reqConfig.cacheKey, reqConfig.cacheTime, reqConfig);
76+
77+
if (cached) {
78+
return cached;
79+
}
80+
}
81+
6682
const fetcherConfig = buildConfig<
6783
ResponseData,
6884
RequestBody,

test/benchmarks/fetchf.bench.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ const requestWithCache = () =>
3737
strategy: 'softFail',
3838
});
3939

40+
const requestWithStringCache = () =>
41+
fetchf('https://api.example.com/posts/1', {
42+
cacheKey: 'post-1',
43+
cacheTime: 300,
44+
strategy: 'softFail',
45+
});
46+
4047
suite
4148
.add('Simple fetchf request', () => {
4249
return simpleRequest();
@@ -47,6 +54,9 @@ suite
4754
.add('fetchf with caching enabled', () => {
4855
return requestWithCache();
4956
})
57+
.add('fetchf with string cache key', () => {
58+
return requestWithStringCache();
59+
})
5060
.on('cycle', (event) => {
5161
console.log(String(event.target));
5262
})

0 commit comments

Comments
 (0)