ระดับกลาง
18 พฤศจิกายน 2569หาบั๊กให้เจอ: Node.js / NestJS
โค้ดหนึ่งชิ้น หาบั๊กให้เจอ หรือพิสูจน์ว่าไม่มีบั๊ก
typescript
1@Injectable()
2export class ArticleService {
3 constructor(
4 private redis: Redis,
5 private prisma: PrismaService,
6 ) {}
7
8 async getArticleById(id: string): Promise<Article | null> {
9 const cacheKey = `article:${id}`;
10 const cached = await this.redis.get(cacheKey);
11 if (cached) return JSON.parse(cached);
12 const article = await this.prisma.article.findUnique({ where: { id } });
13 if (!article) return null;
14 await this.redis.set(cacheKey, JSON.stringify(article), 'EX', 600);
15 return article;
16 }
17}คลิกบรรทัดที่เป็นต้นเหตุของปัญหา
คำตัดสินของคุณ
วันละหนึ่งครั้ง คำตอบจะแสดงทันทีหลังจากนั้น
รักษาสถิติต่อเนื่องของคุณ
สร้างบัญชีเพื่อเก็บสถิติต่อเนื่องและติดตามความคืบหน้า