Skip to content

Commit

Permalink
Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
huulbaek committed Oct 30, 2023
1 parent a45c284 commit 2965f6d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM oven/bun:latest

WORKDIR /app
COPY package.json ./
COPY bun.lockb ./

RUN bun install
RUN bun install --no-save

COPY . .

Expand Down
Binary file modified bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions docker-compose-with-redis-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.8'

services:
redis:
container_name: redis
image: redis:latest
ports:
- 6379:6379
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ services:
volumes:
- .:/app
- node_modules:/app/node_modules
command: bun run src/index.ts
command: bun run start
redis:
container_name: redis
image: redis:latest
ports:
- 6379:6379

volumes:
node_modules:
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.ts",
"scripts": {
"dev": "bun run --watch src/index.ts",
"start": "bun install && bun run src/index.ts",
"start": "bun install --no-save && bun run src/index.ts",
"test": "bun test",
"lint": "bun eslint ."
},
Expand All @@ -20,6 +20,10 @@
"typescript": "^5.1.3"
},
"peerDependencies": {
"typescript": "^5.1.3"
"typescript": "^5.1.3",
"redis": "^4.6.10"
},
"dependencies": {
"redis": "^4.6.10"
}
}
22 changes: 21 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { ISpaceConstant } from './calculations/interfaces/space_constant'
import { IConstant } from './calculations/interfaces/constant'
import { IVariable } from './calculations/interfaces/variable'
import Calculator from './calculations/calculator'
import redis from 'redis'
import { RedisClientType } from '@redis/client'

let redisClient: RedisClientType
if (process.env.USE_CACHE_REDIS === '1') {
(async () => {
redisClient = redis.createClient({
url: 'redis://redis:6379',
})
redisClient.on("error", (error) => console.error(`Error : ${error}`))
await redisClient.connect()
})()
}

const headers = {
'Access-Control-Allow-Origin': '*',
Expand All @@ -11,7 +24,6 @@ const headers = {

Bun.serve({
port: process.env.PORT,
// hostname: "localhost",
async fetch(req) {
if (req.method === 'OPTIONS') {
const res = new Response('Ok', { headers })
Expand All @@ -21,6 +33,12 @@ Bun.serve({
if (url.pathname === "/") return new Response("Agiliate is running", { headers })
if (url.pathname === "/calculate") {
const jsonReq = await req.json()
if (process.env.CACHE === '1') {
const cachedResult = await redisClient.get(jsonReq)
if (cachedResult) {
return Response.json(JSON.parse(cachedResult), { headers })
}
}
const variables: IVariable = {
...jsonReq.variables
}
Expand All @@ -32,6 +50,8 @@ Bun.serve({
}
const calculator = new Calculator(variables, customSpaceConstants, customConstants)
const result = calculator.result()
if (process.env.CACHE === '1')
redisClient.set(JSON.stringify(jsonReq), JSON.stringify(result))
return Response.json(result, { headers })
}
return new Response("404!", { headers })
Expand Down

0 comments on commit 2965f6d

Please sign in to comment.