Skip to content

Commit

Permalink
feat: add in-memory cache to github events
Browse files Browse the repository at this point in the history
  • Loading branch information
framp committed Sep 5, 2023
1 parent 47e06cf commit 3bd625e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
8 changes: 6 additions & 2 deletions app/features/providers/github/commands/getPastEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import {
} from '~/features/providers/misc/http'
import { mapIssuesToEvents } from './misc/mapIssueToEvent'
import type { Event } from './types'
import { cache } from './misc/cache'

export const getPastEvents = async (
nextToken?: String
): Promise<ApiResponse<Event[]>> => {
try {
const client = newGraphQLClientFactory()
if (cache.has('pastEvents')) {
return newSuccessfulResponse(cache.get('pastEvents'))
}

const client = newGraphQLClientFactory()
const res = await client<GetPastEventsQuery>(getPastEventsQuery, {
owner: 'cyprus-developer-community',
repo: 'events',
Expand All @@ -23,7 +27,7 @@ export const getPastEvents = async (
})

const events = await mapIssuesToEvents(res.repository.issues.nodes)

cache.set('pastEvents', events, 60 * 60 * 24)
return newSuccessfulResponse(events)
} catch (e) {
return newErrorResponse(e)
Expand Down
7 changes: 6 additions & 1 deletion app/features/providers/github/commands/getUpcomingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import {
} from '~/features/providers/misc/http'
import type { Event } from './types'
import { mapIssuesToEvents } from './misc/mapIssueToEvent'
import { cache } from './misc/cache'

export const getUpcomingEvents = async (
nextToken?: String
): Promise<ApiResponse<Event[]>> => {
try {
const client = newGraphQLClientFactory()
if (cache.has('upcomingEvents')) {
return newSuccessfulResponse(cache.get('upcomingEvents'))
}

const client = newGraphQLClientFactory()
const res = await client<GetUpcomingEventsQuery>(getUpcomingEventsQuery, {
owner: 'cyprus-developer-community',
repo: 'events',
Expand All @@ -23,6 +27,7 @@ export const getUpcomingEvents = async (
})

const events = await mapIssuesToEvents(res.repository.issues.nodes)
cache.set('upcomingEvents', events, 60 * 60 * 24)
return newSuccessfulResponse(events)
} catch (e) {
return newErrorResponse(e)
Expand Down
17 changes: 17 additions & 0 deletions app/features/providers/github/commands/misc/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import NodeCache from 'node-cache'
let cache: NodeCache

declare global {
var __cache: NodeCache | undefined
}

if (process.env.NODE_ENV === 'production') {
cache = new NodeCache()
} else {
if (!global.__cache) {
global.__cache = new NodeCache()
}
cache = global.__cache
}

export { cache }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"date-fns-tz": "^2.0.0",
"graphql": "^16.6.0",
"marked": "^4.2.12",
"node-cache": "^5.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.11",
Expand Down
34 changes: 29 additions & 5 deletions pnpm-lock.yaml

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

0 comments on commit 3bd625e

Please sign in to comment.