Skip to content

Building a cache key

cacheAll subjects

A cache key has one job: two requests that should return different answers must not produce the same key. Every dimension the key forgets is a request served the wrong data, and it will not look like a caching bug when it is reported.

The module under test

1
export function cacheKey(request) {
2
const parts = [request.resource];
3
if (request.locale) {
4
parts.push("locale=" + request.locale);
5
}
6
if (request.userId) {
7
parts.push("user=" + request.userId);
8
}
9
const filters = Object.keys(request.filters || {}).sort();
10
for (const name of filters) {
11
parts.push(name + "=" + request.filters[name]);
12
}
13
return parts.join("|");
14
}
15

Your tests

Mutants

Mutation score
Survived
0
Killed
0 / 0
Line coverage
Tests
0
Assertion width
Cycle
    survivedkilledtimed outequivalentnot run