Skip to content

Commit f88d6fe

Browse files
committed
use hash of gql query as part of the cache key
1 parent 2c290bb commit f88d6fe

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

src/resolvers/Story/author.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import axios from 'axios';
33
export function getResolver(cache: any): any {
44
const authorResolver = async parent => {
55
const userCacheKey = `user:${parent.by}`;
6-
const hasKey = await cache.has(userCacheKey);
76

7+
const hasKey = await cache.has(userCacheKey);
88
if (hasKey) {
99
return cache.get(userCacheKey);
1010
}
@@ -14,7 +14,7 @@ export function getResolver(cache: any): any {
1414

1515
cache.put(userCacheKey, resp.data, 3600);
1616

17-
return new Promise(resolve => resolve(resp.data));
17+
return new Promise(resolve => resolve(cache.get(userCacheKey)));
1818
};
1919

2020
return authorResolver;

src/resolvers/jobs.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { Cache } from '@/lib/cache/Cache';
22
import axios from 'axios';
3+
const hash = require('object-hash');
34

45
export function getResolver(storyIds, stories, cache: Cache): any {
5-
const result = async (_, { first, skipText }) => {
6+
const result = async (_, { first, skipText }, { req }) => {
67
const kind = 'job';
78

89
storyIds.splice(0, storyIds.length);
910
stories.splice(0, stories.length);
1011

12+
const queryHash = hash(req.body);
13+
1114
const ids: number[] = [];
12-
const cacheKey = `jobstoryids:${first}-${skipText ? 'skipText' : 'dontSkipText'}`;
15+
//const cacheKey = `jobstoryids:${first}-${skipText ? 'skipText' : 'dontSkipText'}`;
16+
const cacheKey = `jobstoryids:${first}:${queryHash}`;
17+
18+
console.log('cacheKey = ', cacheKey);
1319

1420
const hasKey = await cache.has(cacheKey);
21+
1522
if (!hasKey) {
1623
console.log(`getting ${kind}storyids from URL...`);
1724

src/resolvers/stories.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import axios from 'axios';
2+
const hash = require('object-hash');
23

34
export function getResolver(topStoryIds, topStories, cache: any): any {
4-
const stories = async (_, { first, kind }) => {
5+
const stories = async (_, { first, kind }, { req }) => {
56
topStoryIds.splice(0, topStoryIds.length);
67
topStories.splice(0, topStories.length);
78

89
kind = `${kind}`.toLowerCase();
10+
const queryHash = hash(req.body);
911

1012
const ids: number[] = [];
11-
const topstoryCacheKey = `${kind}storyids:${first}`;
13+
const storyIdsCacheKey = `${kind}storyids:${first}:${queryHash}`;
1214

13-
const hasTopStoryKey = await cache.has(topstoryCacheKey);
14-
if (!hasTopStoryKey) {
15+
console.log('storyIdsCacheKey = ', storyIdsCacheKey);
16+
17+
const hasStoryIdsKey = await cache.has(storyIdsCacheKey);
18+
19+
if (!hasStoryIdsKey) {
1520
console.log(`getting ${kind}storyids from URL...`);
1621

1722
const resp = await axios.get(`${process.env.HACKERNEWS_API_URL}/${kind}stories.json?limitToFirst=${first}&orderBy="$key"`);
@@ -25,11 +30,11 @@ export function getResolver(topStoryIds, topStories, cache: any): any {
2530

2631
const cacheTtl = cacheTtlMap[kind] || 60;
2732

28-
cache.put(topstoryCacheKey, data, cacheTtl);
33+
cache.put(storyIdsCacheKey, data, cacheTtl);
2934
ids.push(...data);
3035
}
3136

32-
const cacheData = await cache.get(topstoryCacheKey);
37+
const cacheData = await cache.get(storyIdsCacheKey);
3338

3439
ids.push(...cacheData);
3540
ids.splice(first);

tests/resolvers/Story/author.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ it('it retrieves the user data and returns it', async () => {
2323
it('it retrieves the user data and caches it', async () => {
2424
await resolver({ by: 'test' });
2525

26-
expect(cache.has('user:test')).toBeTruthy();
26+
expect(await cache.has('user:test')).toBeTruthy();
2727
});

tests/resolvers/__snapshots__/stories.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Object {
5555
"url": "https://twitter.com/ID_AA_Carmack/status/1400930510671601666",
5656
},
5757
},
58-
"newstoryids:2": Object {
58+
"newstoryids:2:2958f564f361b193b44e212175b8874c8537b9e9": Object {
5959
"expires": 0,
6060
"ts": 0,
6161
"value": Array [

tests/resolvers/comments.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ it('it retrieves the comment data and returns it', async () => {
2222
it('it retrieves the comment data and caches it', async () => {
2323
await resolver({ kids: [27397733] }, { first: 1 });
2424

25-
expect(cache.has('comment:27397733')).toBeTruthy();
25+
expect(await cache.has('comment:27397733')).toBeTruthy();
2626
});

tests/resolvers/stories.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { MemoryCache } from '@/lib/cache/MemoryCache';
44
import { getResolver } from '@/resolvers/stories';
5+
const hash = require('object-hash');
56

67
let cache: MemoryCache;
78
let resolver: any;
@@ -16,7 +17,8 @@ beforeEach(() => {
1617
});
1718

1819
it('it retrieves the story data and returns it', async () => {
19-
const data = await resolver({}, { first: 2, kind: 'NEW' });
20+
const req = { body: { first: 2, kind: 'NEW', a: 1, b: 2 } };
21+
const data = await resolver({}, { first: 2, kind: 'NEW' }, { req });
2022

2123
for (const item of data) {
2224
//item.ts = 0;
@@ -27,13 +29,17 @@ it('it retrieves the story data and returns it', async () => {
2729
});
2830

2931
it('it retrieves the user data and caches it', async () => {
30-
await resolver({}, { first: 2, kind: 'NEW' });
32+
const req = { body: { query: 'abc', vars: 'bbb' } };
33+
const reqHash = hash(req.body);
3134

32-
expect(cache.has('newstoryids:2')).toBeTruthy();
35+
await resolver({}, { first: 2, kind: 'NEW' }, { req });
36+
37+
expect(await cache.has(`newstoryids:2:${reqHash}`)).toBeTruthy();
3338
});
3439

3540
it('it retrieves each story and caches it', async () => {
36-
const data = await resolver({}, { first: 2, kind: 'NEW' });
41+
const req = { body: { first: 2, kind: 'NEW', a: 1, b: 2 } };
42+
const data = await resolver({}, { first: 2, kind: 'NEW' }, { req });
3743

3844
data.forEach(item => {
3945
expect(cache.has(`newstory:${item.id}`)).toBeTruthy();

0 commit comments

Comments
 (0)