Skip to content

Commit

Permalink
feat(caching): add caching for GET, POST, DELETE, and PUT requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cacheflow committed Mar 29, 2020
1 parent 5322350 commit f7f25e1
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 54 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/form-data": "^2.2.1",
"@types/node-fetch": "^2.3.2",
"form-data": "^2.3.3",
"lru-cache": "^5.1.1",
"node-fetch": "^2.3.0"
},
"husky": {
Expand Down
85 changes: 75 additions & 10 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// We use the real `Response` constructor from the actual node-fetch module
const { Response } = require.requireActual('node-fetch');

// And then mock `fetch` going forward using `Response` with mock data
jest.mock('node-fetch', () =>
jest.fn(() =>
Expand All @@ -14,21 +15,23 @@ const fetch = require.requireMock('node-fetch');
import { Client } from '../client';
import FormData from 'form-data';
import createForm from '../helpers/createForm';
import * as cache from '../cache';

beforeEach(() => {
fetch.mockReset();
jest.clearAllMocks();
});

test('Client instantiates with access token', () => {
const client = new Client({ access_token: 'abc123' });
expect(client.access_token).toBe('abc123');
const client = new Client({ access_token: 'abc1234' });
expect(client.access_token).toBe('abc1234');
expect(client.client_id).toBeUndefined();
expect(client.anonymous).toBe(false);
});

test('Client instantiates with client ID', () => {
const client = new Client({ client_id: 'abc123' });
expect(client.client_id).toBe('abc123');
const client = new Client({ client_id: 'abc12345' });
expect(client.client_id).toBe('abc12345');
expect(client.access_token).toBeUndefined();
expect(client.anonymous).toBe(true);
});
Expand Down Expand Up @@ -57,11 +60,11 @@ test('requests are decorated with an access token', async () => {
test('requests are decorated with a client ID', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ client_id: 'abc123' });
await client.request('https://www.blah.com', {
await client.request('https://www.blahs.com', {
method: 'GET',
});
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith('https://www.blah.com', {
expect(fetch).toHaveBeenCalledWith('https://www.blahs.com', {
headers: {
Authorization: 'Client-ID abc123',
},
Expand Down Expand Up @@ -119,12 +122,74 @@ test('post() has correct headers and passed in form data reference', async () =>
});
});

test('calls cacheGet when calling request', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
const params = {
image: 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
type: 'base64',
};

const cacheGetSpy = jest.spyOn(cache, 'cacheGet');

const form = createForm(params);

await client.post('https://www.abc.com', form);
expect(cacheGetSpy).toHaveBeenCalledTimes(1);
});

test('calls cacheSet when calling request', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
const params = {
image: 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
type: 'base64',
};

const cacheSetSpy = jest.spyOn(cache, 'cacheSet');

const form = createForm(params);

await client.post('https://www.abc.com', form);
expect(cacheSetSpy).toHaveBeenCalledTimes(1);
});

test('fetches from the cache on subsequent post requests', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
const params = {
image: 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
type: 'base64',
};

const cacheSetSpy = jest.spyOn(cache, 'cacheSet');

const form = createForm(params);

await client.post('https://www.abc.com', form);
await client.post('https://www.abc.com', form);
await client.post('https://www.abc.com', form);

expect(cacheSetSpy).toHaveBeenCalledTimes(1);
});

test('fetches from the cache on subsequent get requests', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
const cacheSetSpy = jest.spyOn(cache, 'cacheSet');
await client.get('https://www.txyz.com');
await client.get('https://www.txyz.com');
await client.get('https://www.txyz.com');

expect(cacheSetSpy).toHaveBeenCalledTimes(1);
});

test('get() has correct headers', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
await client.get('https://www.blah.com');
await client.get('https://www.xyz.com');
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith('https://www.blah.com', {
expect(fetch).toHaveBeenCalledWith('https://www.xyz.com', {
headers: {
Authorization: 'Bearer abc123',
},
Expand All @@ -135,9 +200,9 @@ test('get() has correct headers', async () => {
test('delete() has correct headers', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
await client.delete('https://www.blah.com');
await client.delete('https://www.mlop.com');
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith('https://www.blah.com', {
expect(fetch).toHaveBeenCalledWith('https://www.mlop.com', {
headers: {
Authorization: 'Bearer abc123',
},
Expand Down
24 changes: 24 additions & 0 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';

const LRU = require('lru-cache');

const options = {
length: function(n: number, key: any) {
return n * 2 + key.length;
},
maxAge: 1000 * 30 * 30,
};

export const cache = new LRU(options);

export const cacheGet = (key: string) => {
return cache.get(key) || false;
};

export const cacheSet = (
key: string,
res: Promise<ImgurApiResponse | AuthenticationRequiredResponse>,
) => {
cache.set(key, JSON.stringify(res));
return res;
};
27 changes: 23 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fetch, { RequestInit } from 'node-fetch';
import FormData from 'form-data';
import createForm from './helpers/createForm';

import { hashUrl } from './helpers/hashUrl';
import { cacheGet, cacheSet } from './cache';
export interface ClientOptions {
access_token?: string;
client_id?: string;
Expand All @@ -13,6 +14,8 @@ export type PostOrPutData =
}
| FormData;

export type FormDataString = string;

export class Client {
public readonly access_token: string | undefined;
public readonly client_id: string | undefined;
Expand Down Expand Up @@ -50,8 +53,24 @@ export class Client {
};

try {
const params = Object.entries(headers)
.map((key: any, val: any) => {
return `${encodeURIComponent(key)}=${encodeURIComponent(val)}`;
})
.join('&');
const hashKey = hashUrl(`${endpoint}${params}`);
const cacheData = cacheGet(hashKey);

if (cacheData) {
return cacheData;
}

const response = await fetch(endpoint, { ...options, headers });
return response.json();
const json = await response.json();

cacheSet(hashKey, json);

return json;
} catch (err) {
return Promise.reject(new Error(err.message));
}
Expand Down Expand Up @@ -93,7 +112,6 @@ export class Client {
} else {
form = createForm(params);
}

return this.request(endpoint, {
method: 'POST',
headers: form.getHeaders(),
Expand All @@ -108,9 +126,10 @@ export class Client {
* @returns A JSON response object
*/
get(endpoint: string): Promise<any> {
return this.request(endpoint, {
const res = this.request(endpoint, {
method: 'GET',
});
return res;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/hashUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const hashUrl = function(str: string, seed: number = 0) {
let h1 = 0xdeadbeef ^ seed;
let h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 =
Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 =
Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString();
};
48 changes: 8 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3369,7 +3369,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4466,7 +4466,7 @@ libnpm@^2.0.1:
read-package-json "^2.0.13"
stringify-package "^1.0.0"

libnpmaccess@*, libnpmaccess@^3.0.1:
libnpmaccess@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/libnpmaccess/-/libnpmaccess-3.0.1.tgz#5b3a9de621f293d425191aa2e779102f84167fa8"
integrity sha512-RlZ7PNarCBt+XbnP7R6PoVgOq9t+kou5rvhaInoNibhPO7eMlRfS0B8yjatgn2yaHIwWNyoJDolC/6Lc5L/IQA==
Expand Down Expand Up @@ -4495,7 +4495,7 @@ libnpmhook@^5.0.2:
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmorg@*, libnpmorg@^1.0.0:
libnpmorg@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/libnpmorg/-/libnpmorg-1.0.0.tgz#979b868c48ba28c5820e3bb9d9e73c883c16a232"
integrity sha512-o+4eVJBoDGMgRwh2lJY0a8pRV2c/tQM/SxlqXezjcAg26Qe9jigYVs+Xk0vvlYDWCDhP0g74J8UwWeAgsB7gGw==
Expand All @@ -4520,7 +4520,7 @@ libnpmpublish@^1.1.0:
semver "^5.5.1"
ssri "^6.0.1"

libnpmsearch@*, libnpmsearch@^2.0.0:
libnpmsearch@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/libnpmsearch/-/libnpmsearch-2.0.1.tgz#eccc73a8fbf267d765d18082b85daa2512501f96"
integrity sha512-K0yXyut9MHHCAH+DOiglQCpmBKPZXSUu76+BE2maSEfQN15OwNaA/Aiioe9lRFlVFOr7WcuJCY+VSl+gLi9NTA==
Expand All @@ -4529,7 +4529,7 @@ libnpmsearch@*, libnpmsearch@^2.0.0:
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmteam@*, libnpmteam@^1.0.1:
libnpmteam@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/libnpmteam/-/libnpmteam-1.0.1.tgz#ff704b1b6c06ea674b3b1101ac3e305f5114f213"
integrity sha512-gDdrflKFCX7TNwOMX1snWojCoDE5LoRWcfOC0C/fqF7mBq8Uz9zWAX4B2RllYETNO7pBupBaSyBDkTAC15cAMg==
Expand Down Expand Up @@ -4675,11 +4675,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4688,33 +4683,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -4760,11 +4733,6 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down Expand Up @@ -5476,7 +5444,7 @@ npm-pick-manifest@^2.2.3:
npm-package-arg "^6.0.0"
semver "^5.4.1"

npm-profile@*, npm-profile@^4.0.1:
npm-profile@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/npm-profile/-/npm-profile-4.0.1.tgz#d350f7a5e6b60691c7168fbb8392c3603583f5aa"
integrity sha512-NQ1I/1Q7YRtHZXkcuU1/IyHeLy6pd+ScKg4+DQHdfsm769TGq6HPrkbuNJVJS4zwE+0mvvmeULzQdWn2L2EsVA==
Expand Down Expand Up @@ -6420,7 +6388,7 @@ readable-stream@~1.1.10:
isarray "0.0.1"
string_decoder "~0.10.x"

readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=
Expand Down

0 comments on commit f7f25e1

Please sign in to comment.