Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): prefer If-Modified-Since over If-None-Match #27394

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/util/cache/repository/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ export interface BranchCache {
}

export interface HttpCache {
etag: string;
etag?: string;
httpResponse: HttpResponse<unknown>;
lastModified?: string;
timeStamp: string;
}

Expand Down
48 changes: 48 additions & 0 deletions lib/util/http/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,54 @@ describe('util/http/index', () => {
});
});

it('uses last-modified header for caching', async () => {
httpMock
.scope(baseUrl, {
reqheaders: {
accept: 'application/json',
},
})
.get('/')
.reply(200, '{ "test": true }', {
'last-modified': 'Sun, 18 Feb 2024 18:00:05 GMT',
});
expect(
await http.getJson('http://renovate.com', { repoCache: true }),
).toEqual({
authorization: false,
body: {
test: true,
},
headers: {
'last-modified': 'Sun, 18 Feb 2024 18:00:05 GMT',
},
statusCode: 200,
});

httpMock
.scope(baseUrl, {
reqheaders: {
accept: 'application/json',
},
})
.get('/')
.reply(304, '', {
'last-modified': 'Sun, 18 Feb 2024 18:00:05 GMT',
});
expect(
await http.getJson('http://renovate.com', { repoCache: true }),
).toEqual({
authorization: false,
body: {
test: true,
},
headers: {
'last-modified': 'Sun, 18 Feb 2024 18:00:05 GMT',
},
statusCode: 200,
});
});

it('postJson', async () => {
httpMock.scope(baseUrl).post('/').reply(200, {});
expect(
Expand Down
29 changes: 22 additions & 7 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,23 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
// istanbul ignore else: no cache tests
if (!resPromise) {
if (httpOptions.repoCache) {
const cachedEtag = getCache().httpCache?.[url]?.etag;
if (cachedEtag) {
logger.debug(`Using cached etag for ${url}`);
const responseCache = getCache().httpCache?.[url];
// Prefer If-Modified-Since over If-None-Match
if (responseCache?.['lastModified']) {
logger.debug(
`http cache: trying cached Last-Modified "${responseCache?.['lastModified']}" for ${url}`,
);
options.headers = {
...options.headers,
'If-Modified-Since': responseCache['lastModified'],
rarkins marked this conversation as resolved.
Show resolved Hide resolved
};
} else if (responseCache?.etag) {
logger.debug(
`http cache: trying cached etag "${responseCache.etag}" for ${url}`,
);
options.headers = {
...options.headers,
'If-None-Match': cachedEtag,
'If-None-Match': responseCache.etag,
};
}
}
Expand Down Expand Up @@ -259,19 +270,23 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
if (httpOptions.repoCache) {
const cache = getCache();
cache.httpCache ??= {};
if (resCopy.statusCode === 200 && resCopy.headers?.etag) {
if (
resCopy.statusCode === 200 &&
(resCopy.headers?.etag ?? resCopy.headers['last-modified'])
) {
logger.debug(
`Saving response to cache: ${url} with etag ${resCopy.headers.etag}`,
`http cache: saving ${url} (etag=${resCopy.headers.etag}, lastModified=${resCopy.headers['last-modified']})`,
);
cache.httpCache[url] = {
etag: resCopy.headers.etag,
httpResponse: copyResponse(res, deepCopyNeeded),
lastModified: resCopy.headers['last-modified'],
timeStamp: new Date().toISOString(),
};
}
if (resCopy.statusCode === 304 && cache.httpCache[url]?.httpResponse) {
logger.debug(
`Using cached response: ${url} with etag ${resCopy.headers.etag} from ${cache.httpCache[url].timeStamp}`,
`http cache: Using cached response: ${url} from ${cache.httpCache[url].timeStamp}`,
);
const cacheCopy = copyResponse(
cache.httpCache[url].httpResponse,
Expand Down
Loading