Skip to content

Commit

Permalink
added headers argument to cacheKeyFn to enable using headers as part …
Browse files Browse the repository at this point in the history
…of the cache key (#59)
  • Loading branch information
markusn authored Sep 24, 2024
1 parent 2885ac1 commit 7e1f8dd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 63 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

# 5.5.0

* Added a third argument to `cacheKeyFn` containing the headers to enable using headers as part
of the cache key.
* Removed unused `url` param from `handleRedirect`.

# 5.4.2
* Ignore .DS_Store and document fetch retry and timeout config in README.
* Bump deps to fix security audit.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Note: these options are used for the `fetchBuilder`, not the `fetch`-function

* `agent`: (default: null), keepAlive Agent instance.
* `cache`: (default: `an instance of AsyncCache`) (https://github.com/ExpressenAB/exp-asynccache). To disable caching set `{cache: null}`
* `cacheKeyFn`: (default: caches on the url + sha1 of the body) An optional formatting function for finding the cache-key. One might, for example, want to cache on an url with the get params stripped.
* `cacheKeyFn`: (default: caches on the url + sha1 of the body) An optional formatting function for finding the cache-key. One might, for example, want to cache on an url with the get params stripped or use some headers as part of the cache key.
* `cacheNotFound`: (default: false). If set it will cache 404s, if given a number it will cache the 404 for that time. If the `maxAgeFn` is given, it will get this time as the first parameter.
* `cacheValueFn`: (default: caches the response body) An optional function for change what will be returned and cached from fetch.
* `clone`: (default: true), should fetch clone objects before handing them from the cache.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function buildFetch(behavior) {
}

function performRequest(url, headers, explicitTimeout, method, body, redirectCount, callback, onRequestInit) {
const cacheKey = cacheKeyFn(url, body);
const cacheKey = cacheKeyFn(url, body, headers);
const startTime = new Date().getTime();
stats.calls++;
cache.lookup(cacheKey, (resolveFunction) => {
Expand Down
108 changes: 48 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exp-fetch",
"version": "5.4.3",
"version": "5.5.0",
"description": "A small pluggable fetch lib",
"main": "index.js",
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions test/fetchingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const nock = require("nock");
const fetchBuilder = require("../.");
const crypto = require("crypto");

describe("fetch", () => {
const host = "http://example.com";
Expand Down Expand Up @@ -452,6 +453,27 @@ describe("fetch", () => {
}, done);
});

it("should cache with a lookup function using the headers", (done) => {
fake.get(path).matchHeader("foo", "foo").reply(200, { some: "content" }, { "cache-control": "max-age=30" });
fake.get(path).matchHeader("foo", "foo2").reply(200, { some: "content2" }, { "cache-control": "max-age=30" });
function cacheKeyFn(url, body, headers) {
const cacheKey = crypto.createHash("md5").update(`${url} ${headers.foo}`).digest("hex");
return cacheKey;
}

const fetch = fetchBuilder({ cacheKeyFn }).fetch;
Promise.all([
fetch({ url: host + path, headers: { foo: "foo" } }),
fetch({ url: host + path, headers: { foo: "foo2" } }),
fetch({ url: host + path, headers: { foo: "foo2" } }),
fetch({ url: host + path, headers: { foo: "foo" } }),
]).then((result) => {
expect(result[0]).to.deep.equal({ some: "content" }).eql(result[3]);
expect(result[1]).to.deep.equal({ some: "content2" }).eql(result[2]);
done();
}, done);
});

it("should cache with a custom value function", (done) => {
fake.get(path).reply(200, { some: "content" }, { "cache-control": "max-age=30" });
const valueFn = function (body, headers, statusCode) {
Expand Down

0 comments on commit 7e1f8dd

Please sign in to comment.