Skip to content

Commit

Permalink
Debugging Upstash #2
Browse files Browse the repository at this point in the history
  • Loading branch information
david emioma committed Apr 2, 2024
1 parent 5248930 commit 866b2d9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/api/cart/[cartItemId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from "zod";
import { redis } from "@/lib/redis";
import prismadb from "@/lib/prisma";
import { UserRole } from "@prisma/client";
import { redisServer } from "@/lib/redis";
import { NextResponse } from "next/server";
import { currentRole, currentUser } from "@/lib/auth";

Expand Down Expand Up @@ -131,7 +131,7 @@ export async function PATCH(
},
});

// await redis.set(`${user.id}-cart`, newCart);
await redisServer.set(`${user.id}-cart`, JSON.stringify(newCart));

return NextResponse.json({ message: "Cart item updated!" });
} catch (err) {
Expand Down Expand Up @@ -214,7 +214,7 @@ export async function DELETE(
},
});

// await redis.set(`${user.id}-cart`, newCart);
await redisServer.set(`${user.id}-cart`, JSON.stringify(newCart));

return NextResponse.json({ message: "Cart item deleted!" });
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions app/api/cart/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { redis } from "@/lib/redis";
import prismadb from "@/lib/prisma";
import { UserRole } from "@prisma/client";
import { redisServer } from "@/lib/redis";
import { NextResponse } from "next/server";
import { currentRole, currentUser } from "@/lib/auth";
import { CartItemSchema } from "@/lib/validators/cart-item";
Expand Down Expand Up @@ -168,7 +168,7 @@ export async function POST(request: Request) {
},
});

await redis.set(`${user.id}-cart`, newCart);
await redisServer.set(`${user.id}-cart`, JSON.stringify(newCart));

return NextResponse.json({ message: "Item added to cart!" });
} catch (err) {
Expand All @@ -193,12 +193,12 @@ export async function GET(request: Request) {
return NextResponse.json({ cart: null });
}

const cachedCart = await redis.get(`${user.id}-cart`);
const cachedCart = await redisServer.get(`${user.id}-cart`);

if (cachedCart) {
console.log("Cached cart was returned");

return NextResponse.json(cachedCart);
return NextResponse.json(JSON.parse(cachedCart));
}

const cart = await prismadb.cart.findUnique({
Expand Down Expand Up @@ -227,7 +227,7 @@ export async function GET(request: Request) {
},
});

await redis.set(`${user.id}-cart`, cart);
await redisServer.set(`${user.id}-cart`, JSON.stringify(cart));

return NextResponse.json(cart);
} catch (err) {
Expand Down
17 changes: 14 additions & 3 deletions lib/redis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Redis } from "@upstash/redis";
import Redis from "ioredis";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis as RedisClient } from "@upstash/redis";

export const redis = new Redis({
export const redis = new RedisClient({
url: process.env.UPSTASH_REDIS_REST_URL as string,
token: process.env.UPSTASH_REDIS_REST_TOKEN as string,
});

export const apiRatelimit = new Ratelimit({
redis: redis ?? Redis.fromEnv(),
redis: redis ?? RedisClient.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "1 m"),
});

const getRedisUrl = () => {
if (process.env.REDIS_URL) {
return process.env.REDIS_URL as string;
}

throw new Error("Redis URL not specified!");
};

export const redisServer = new Redis(getRedisUrl());
79 changes: 79 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"date-fns": "^3.3.1",
"dotenv": "^16.4.5",
"firebase": "^10.7.2",
"ioredis": "^5.3.2",
"lucide-react": "^0.319.0",
"next": "14.1.0",
"next-auth": "^5.0.0-beta.4",
Expand Down

0 comments on commit 866b2d9

Please sign in to comment.