Skip to content

Commit

Permalink
fix: cache is now properly invalidated when packs are modified
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNuclearNexus committed Sep 13, 2024
1 parent ee27307 commit eb2b26e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
16 changes: 15 additions & 1 deletion platforms/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { HTTPResponses } from "data-types"
import abCache from "abstract-cache"
import IORedis from "ioredis"
import { Client } from "typesense"
import { resolve } from "path"
import { rejects } from "assert"

export let TYPESENSE_APP: Client

Expand Down Expand Up @@ -64,7 +66,7 @@ export let REDIS: IORedis | undefined = undefined

async function registerCacheRedis() {
const redis = new IORedis({
host: process.env.DOCKER ? "redis" : "127.0.0.1",
host: process.env.DOCKER === "true" ? "redis" : "127.0.0.1",
})
REDIS = redis

Expand Down Expand Up @@ -124,6 +126,8 @@ export async function setupApp() {
methods: ["GET", "PUT", "POST", "PATCH", "DELETE"],
})

console.log(API_APP.cache)

// API_APP.addHook('preHandler', (request, reply, done) => {
// reply.header("Access-Control-Allow-Origin", "*");
// reply.header("Access-Control-Allow-Methods", "POST, GET, PUT, PATCH, OPTIONS, DELETE");
Expand Down Expand Up @@ -166,3 +170,13 @@ export async function set(
})
})
}

export async function invalidate(pattern: string): Promise<void> {
if (REDIS === undefined)
return

const matches = await REDIS.keys(pattern)
if (matches.length === 0)
return
const deleted = await REDIS.del(matches)
}
12 changes: 9 additions & 3 deletions platforms/api/src/routes/packs/id/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox"
import { API_APP, get, sendError, set } from "../../../app.js"
import { API_APP, get, invalidate, sendError, set } from "../../../app.js"
import { getStorage } from "firebase-admin/storage"
import {
HTTPResponses,
Expand Down Expand Up @@ -306,8 +306,7 @@ const setPack = async (request: any, reply: any) => {
)
}

const requestIdentifier = "GET-PACK::" + packId
await set(requestIdentifier, undefined, 1)
invalidateCachedData(packData.id, doc.id)

request.log.info(packData)
await doc.ref.set({ data: packData }, { merge: true })
Expand Down Expand Up @@ -731,3 +730,10 @@ API_APP.route({
)
},
})

export function invalidateCachedData(id: string | undefined, docId: string) {
if (id !== undefined)
invalidate("**" + id + "**")
invalidate("**" + docId + "**")
}

7 changes: 7 additions & 0 deletions platforms/api/src/routes/packs/id/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "data-types"
import { coerce, compare } from "semver"
import { getPackDoc, validateToken } from "database"
import { invalidateCachedData } from "./index.js"

/*
* @route GET /packs/:id/versions
Expand Down Expand Up @@ -140,6 +141,8 @@ API_APP.route({
versionData.name = versionId
versions.push(versionData)

invalidateCachedData(await doc.get("data.id"), doc.id)

await doc.ref.set(
{
data: {
Expand Down Expand Up @@ -256,6 +259,8 @@ API_APP.route({
{ merge: true }
)

invalidateCachedData(await doc.get("data.id"), doc.id)

return reply
.status(HTTPResponses.CREATED)
.send(`Version ${versionId} successfully updated`)
Expand Down Expand Up @@ -345,6 +350,8 @@ API_APP.route({
{ merge: true }
)

invalidateCachedData(await doc.get("data.id"), doc.id)

return reply
.status(HTTPResponses.CREATED)
.send(`Version ${versionId} successfully deleted`)
Expand Down

0 comments on commit eb2b26e

Please sign in to comment.