Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"formik": "^2.4.2",
"next": "13.1.6",
"next-auth": "^4.19.0",
"node-cache": "^5.1.2",
"prisma-json-types-generator": "^2.3.1",
"react": "18.2.0",
"react-apexcharts": "^1.4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/routers/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import yahooFinance from 'yahoo-finance2'
import { sectorEnum } from '../../../utils/enums'
import { FOSSIL_FUEL_INDUSTRIES } from '../../../utils/constants'
import { ContentWrapper } from '../../../utils/content'
import { createTRPCRouter, publicProcedure } from '../trpc'
import { createTRPCRouter, publicProcedure, cachedProcedure } from '../trpc'
import type { IndustryEntry, SectorEntry } from '../../../types'

interface SortOrder {
Expand Down Expand Up @@ -243,7 +243,7 @@ export const companyRouter = createTRPCRouter({
}
}),

getNetAssetValBySector: publicProcedure.query(({ ctx }) => {
getNetAssetValBySector: cachedProcedure.query(({ ctx }) => {
return ctx.prisma.company.groupBy({
by: ['sector'],
_sum: {
Expand Down
61 changes: 61 additions & 0 deletions src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { type Session } from 'next-auth'
import type { inferAsyncReturnType } from '@trpc/server'
import { initTRPC, TRPCError } from '@trpc/server'
import superjson from 'superjson'
import NodeCache from 'node-cache'

import { getServerAuthSession } from '../auth'
import { prisma } from '../db'
Expand All @@ -28,6 +29,13 @@ type CreateContextOptions = {
session: Session | null
}

// Create Cache
const cacheSingleton = new NodeCache()

// A map of cached procedure names to a callable that gives a TTL in seconds
const cachedProcedures: Map<string, (() => number) | undefined> = new Map()
cachedProcedures.set('companyRouter.getNetAssetValBySector', () => 2 * 3600) // 2 hours

/**
* This helper generates the "internals" for a tRPC context. If you need to use
* it, you can export it from here.
Expand Down Expand Up @@ -117,6 +125,49 @@ const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
})
})

const middlewareMarker = 'middlewareMarker' as 'middlewareMarker' & {
__brand: 'middlewareMarker'
}

/**
* Cache middleware that checks cache before running the procedure.
*/
const checkCache = t.middleware(async ({ ctx, next, path, type, rawInput }) => {
if (type !== 'query' || !cachedProcedures.has(path)) {
return next()
}
let key = path
if (rawInput) {
key += JSON.stringify(rawInput).replace(/\"/g, "'")
}

const cachedData = cacheSingleton.get(key)
if (cachedData) {
console.log('Previously cached!')
return {
ok: true,
data: cachedData,
ctx,
marker: middlewareMarker,
}
}
const result = await next()

console.log('USE CACHE!')
console.log(result)

// data is not defined in the type MiddlewareResult
const dataCopy = structuredClone(result)

const ttlSecondsCallable = cachedProcedures.get(path)
if (ttlSecondsCallable) {
cacheSingleton.set(key, dataCopy, ttlSecondsCallable())
} else {
cacheSingleton.set(key, dataCopy)
}
return result
})

/**
* Protected (authenticated) procedure
*
Expand All @@ -127,3 +178,13 @@ const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed)

/**
* Cached procedure
*
* If you want a query or mutation to always check for cached response, use
* this. It checks
*
* @see https://trpc.io/docs/procedures
*/
export const cachedProcedure = t.procedure.use(checkCache)
Loading