重构:将 "lobster" 重命名为 "claw" 并添加国际化支持 (i18n)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import crypto from "crypto";
|
||||
import { db } from "@/lib/db";
|
||||
import { lobsters } from "@/lib/db/schema";
|
||||
import { claws } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { redis } from "@/lib/redis";
|
||||
|
||||
@@ -12,34 +12,34 @@ export function generateApiKey(): string {
|
||||
|
||||
export async function validateApiKey(apiKey: string) {
|
||||
try {
|
||||
const cacheKey = `lobster:key:${apiKey}`;
|
||||
const cachedLobsterId = await redis.get(cacheKey);
|
||||
const cacheKey = `claw:key:${apiKey}`;
|
||||
const cachedClawId = await redis.get(cacheKey);
|
||||
|
||||
if (cachedLobsterId) {
|
||||
const lobster = await db
|
||||
if (cachedClawId) {
|
||||
const claw = await db
|
||||
.select()
|
||||
.from(lobsters)
|
||||
.where(eq(lobsters.id, cachedLobsterId))
|
||||
.from(claws)
|
||||
.where(eq(claws.id, cachedClawId))
|
||||
.limit(1);
|
||||
|
||||
if (lobster.length > 0) {
|
||||
return lobster[0];
|
||||
if (claw.length > 0) {
|
||||
return claw[0];
|
||||
}
|
||||
}
|
||||
|
||||
const lobster = await db
|
||||
const claw = await db
|
||||
.select()
|
||||
.from(lobsters)
|
||||
.where(eq(lobsters.apiKey, apiKey))
|
||||
.from(claws)
|
||||
.where(eq(claws.apiKey, apiKey))
|
||||
.limit(1);
|
||||
|
||||
if (lobster.length === 0) {
|
||||
if (claw.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
await redis.set(cacheKey, lobster[0].id, "EX", API_KEY_CACHE_TTL);
|
||||
await redis.set(cacheKey, claw[0].id, "EX", API_KEY_CACHE_TTL);
|
||||
|
||||
return lobster[0];
|
||||
return claw[0];
|
||||
} catch (error) {
|
||||
console.error("Failed to validate API key:", error);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user