Skip to content

Commit

Permalink
Merge pull request #18 from cgsvv/fix_redis
Browse files Browse the repository at this point in the history
removed redis
  • Loading branch information
cgsvv committed May 9, 2023
2 parents c9a7c12 + 3502ce4 commit c66dc11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 12 additions & 11 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { ratelimit } from "./lib/upstash"
import { isDev } from "./utils/env";
import { digestMessage } from "./utils/fp";

const redis = Redis.fromEnv();
//const redis = Redis.fromEnv();

export async function middleware(req: NextRequest, context: NextFetchEvent) {
const { sentences, targetLang, srcLang, apiKey } = await req.json();
let rkey = `${targetLang}_${srcLang}_${sentences}}`;
rkey = "tranres_" + await digestMessage(rkey);
const cached = await redis.get<string[]>(rkey);
// const cached = await redis.get<string[]>(rkey);

if (!isDev && cached) {
//if (cached) {
console.log("Using cached response " + rkey);
return NextResponse.json(cached);
}
// if (!isDev && cached) {
// //if (cached) {
// console.log("Using cached response " + rkey);
// return NextResponse.json(cached);
// }

// licenseKeys
if (apiKey) {
Expand All @@ -33,10 +33,11 @@ export async function middleware(req: NextRequest, context: NextFetchEvent) {
}
}
// TODO: unique to a user (userid, email etc) instead of IP
const identifier = req.ip ?? "127.0.0.7";
const { success, remaining } = await ratelimit.limit("trans-" + identifier);
console.log(`======== ip ${identifier}, remaining: ${remaining} ========`);
if (!apiKey && !success) {
// const identifier = req.ip ?? "127.0.0.7";
// const { success, remaining } = await ratelimit.limit("trans-" + identifier);
// console.log(`======== ip ${identifier}, remaining: ${remaining} ========`);
// if (!apiKey && !success) {
if (!apiKey) {
return NextResponse.redirect(new URL("/shop", req.url));
}
}
Expand Down
10 changes: 5 additions & 5 deletions pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const config = {
runtime: "edge",
}

const redis = Redis.fromEnv();
// const redis = Redis.fromEnv();

if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
Expand Down Expand Up @@ -39,10 +39,10 @@ export default async function handler(
const result = await OpenAIResult(payload, apiKey);
const resp = parse_gpt_resp(result, res_keys!);

let rkey = `${targetLang}_${srcLang}_${sentences}}`;
rkey = "tranres_" + await digestMessage(rkey);
const data = await redis.set(rkey, JSON.stringify(resp));
console.log("cached data", data);
// let rkey = `${targetLang}_${srcLang}_${sentences}}`;
// rkey = "tranres_" + await digestMessage(rkey);
// const data = await redis.set(rkey, JSON.stringify(resp));
// console.log("cached data", data);

return NextResponse.json(resp);
} catch (error: any) {
Expand Down

1 comment on commit c66dc11

@vercel
Copy link

@vercel vercel bot commented on c66dc11 May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.