Skip to content

Commit 232acd4

Browse files
Deploy: added redis
1 parent 29efaec commit 232acd4

7 files changed

Lines changed: 114 additions & 1 deletion

File tree

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ AWS_SECRET_ACCESS_KEY="password"
66
AWS_REGION="ap-southeast-1"
77
AWS_S3_ENDPOINT="http://localhost:9000"
88
AWS_S3_BUCKET="app"
9-
FORCE_PATH_STYLE="true"
9+
FORCE_PATH_STYLE="true"
10+
11+
REDIS_URL="redis://redis:password@localhost:6379"

docker-compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
name: smu-mods
22

33
services:
4+
redis:
5+
image: redis:7-alpine
6+
container_name: redis
7+
ports:
8+
- 6379:6379
9+
volumes:
10+
- ./data/redis/data:/data
11+
environment:
12+
- REDIS_PASSWORD=password
13+
command: redis-server --requirepass $$REDIS_PASSWORD
14+
415
postgres:
516
image: postgres:17-alpine
617
container_name: postgres

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"react": "^19.0.0",
6666
"react-dom": "^19.0.0",
6767
"recharts": "^2.15.3",
68+
"redis": "^5.8.2",
6869
"sass": "^1.89.1",
6970
"sonner": "^2.0.5",
7071
"superjson": "^2.2.2",

pnpm-lock.yaml

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/env.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ export const env = createEnv({
77
* isn't built with invalid env vars.
88
*/
99
server: {
10+
// Database
1011
DATABASE_URL: z.string().url(),
12+
// AWS
1113
AWS_ACCESS_KEY_ID: z.string(),
1214
AWS_SECRET_ACCESS_KEY: z.string(),
1315
AWS_REGION: z.string(),
1416
AWS_S3_ENDPOINT: z.string().optional(),
1517
AWS_S3_BUCKET: z.string(),
1618
FORCE_PATH_STYLE: z.string().optional(),
19+
// Redis
20+
REDIS_URL: z.string().url(),
1721
},
1822

1923
/**
@@ -22,10 +26,12 @@ export const env = createEnv({
2226
* `NEXT_PUBLIC_`.
2327
*/
2428
client: {
29+
// Next.js
2530
NEXT_PUBLIC_NODE_ENV: z
2631
.enum(["development", "test", "production"])
2732
.default("development"),
2833
NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA: z.string().default("development"),
34+
// PostHog
2935
NEXT_PUBLIC_POSTHOG_KEY: z.string().default("development"),
3036
NEXT_PUBLIC_POSTHOG_HOST: z.string().default("development"),
3137
},
@@ -35,16 +41,22 @@ export const env = createEnv({
3541
* middlewares) or client-side so we need to destruct manually.
3642
*/
3743
runtimeEnv: {
44+
// Database
3845
DATABASE_URL: process.env.DATABASE_URL,
46+
// AWS
3947
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
4048
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
4149
AWS_REGION: process.env.AWS_REGION,
4250
AWS_S3_ENDPOINT: process.env.AWS_S3_ENDPOINT,
4351
AWS_S3_BUCKET: process.env.AWS_S3_BUCKET,
4452
FORCE_PATH_STYLE: process.env.FORCE_PATH_STYLE,
53+
// Redis
54+
REDIS_URL: process.env.REDIS_URL,
55+
// Next.js
4556
NEXT_PUBLIC_NODE_ENV: process.env.NODE_ENV,
4657
NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA:
4758
process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA,
59+
// PostHog
4860
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
4961
NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST,
5062
},

src/server/lib/redis.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createClient } from "redis";
2+
3+
import { env } from "@/env";
4+
5+
export const redis = createClient({
6+
url: env.REDIS_URL,
7+
});

src/server/utils/getModuleJson.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import { GetObjectCommand } from "@aws-sdk/client-s3";
22

33
import { ModuleBank } from "@/types/banks/moduleBank";
44

5+
import { redis } from "../lib/redis";
56
import { S3_BUCKET, s3Client } from "../lib/s3";
67

78
export async function getModuleJson() {
89
try {
10+
await redis.connect();
11+
const cachedData = await redis.get("modules");
12+
if (cachedData) {
13+
redis.destroy();
14+
const jsonData = JSON.parse(cachedData);
15+
return jsonData as ModuleBank;
16+
}
917
const command = new GetObjectCommand({
1018
Bucket: S3_BUCKET,
1119
Key: "modules.json",
@@ -23,6 +31,9 @@ export async function getModuleJson() {
2331
// Parse JSON
2432
const jsonData = JSON.parse(bodyString);
2533

34+
await redis.set("modules", JSON.stringify(jsonData));
35+
redis.destroy();
36+
2637
return jsonData as ModuleBank;
2738
} catch (error) {
2839
console.error("Error downloading and parsing JSON from S3:", error);

0 commit comments

Comments
 (0)