Skip to content

Commit

Permalink
Optimize cache key calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed May 18, 2023
1 parent 09581c0 commit 9ec1c59
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/util/http/index.ts
Expand Up @@ -153,23 +153,25 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
options = applyAuthorization(options);

// use sha512: https://www.npmjs.com/package/hasha#algorithm
const cacheKey = hasha([
'got-',
JSON.stringify({
url,
headers: options.headers,
method: options.method,
}),
]);
const memCacheKey =
options.memCache !== false &&
!etagCache &&

Check failure on line 158 in lib/util/http/index.ts

View workflow job for this annotation

GitHub Actions / lint

Cannot find name 'etagCache'.

Check failure on line 158 in lib/util/http/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Cannot find name 'etagCache'.
(options.method === 'get' || options.method === 'head')
? hasha([
'got-',
JSON.stringify({
url,
headers: options.headers,
method: options.method,
}),
])
: null;

let resPromise: Promise<HttpResponse<T>> | null = null;

// Cache GET requests unless memCache=false
const useMemCache =
options.memCache !== false &&
(options.method === 'get' || options.method === 'head');

if (useMemCache) {
resPromise = memCache.get(cacheKey);
if (memCacheKey) {
resPromise = memCache.get(memCacheKey);
}

// istanbul ignore else: no cache tests
Expand All @@ -196,8 +198,8 @@ export class Http<Opts extends HttpOptions = HttpOptions> {

resPromise = queuedTask();

if (useMemCache) {
memCache.set(cacheKey, resPromise);
if (memCacheKey) {
memCache.set(memCacheKey, resPromise);
}
}

Expand Down

0 comments on commit 9ec1c59

Please sign in to comment.