๋ฌธ์ ์ ์ :
- ๋ฐ๋ณต์ ์ธ get์์ ์ผ๋ก ์ธํ DB ๋ถํ ๋ฐ์
ํด๊ฒฐ๋ฐฉ๋ฒ :
- ๋ฐ๋ณต์ ์ผ๋ก ์ค๋ get ์์ฒญ ์ db์ ์ ์ํ์ง์๊ณ redis์ ์ ์ฅํ์ฌ ๋ถ๋ฌ์ด.
- redis.ts
import {createClient} from "@redis/client";
export class Redis {
public static async getValue(key: string): Promise<any> {
let result = null;
try {
const client = createClient();
client.on("error", err => console.log("Redis Client Error", err));
await client.connect();
result = await client.get(key);
await client.disconnect();
} catch (e) {
console.log(e);
} finally {
return result;
}
}
public static async setValue(key: string, result: string) {
try {
const client = createClient();
client.on("error", err => console.log("Redis Client Error", err));
await client.connect();
await client.set(key, result, {EX: 10});
await client.disconnect();
} catch (e) {
console.log(e);
} finally {
return result;
}
}
}
- service.ts
async getBanners(){
// redis์ banner list๊ฐ ์๋์ง ๊ฒ์ฌ
const cacheData = await Redis.getValue("getBanners");
// ์์ผ๋ฉด ์บ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํ
if (cacheData) {
return JSON.parse(cacheData);
}
// ๋ฐฐ๋๋ฆฌ์คํธ ๋ถ๋ฌ์ค๋ ์๋น์ค์คํ
...
// ๋ฐฐ๋ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๊ธฐ ์ redis์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ
await Redis.setValue("getBanners", JSON.stringify(banners));
return banners;
}
'etc' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
docker๋ก mysql๋์ฐ๊ธฐ (0) | 2023.01.30 |
---|---|
11์ ์ธํ๋ฐ ํด๊ทผ๊ธธ ๋ฐ-์ #01 Node.js (0) | 2022.12.02 |
UNITHON 9th ํ๊ธฐ (1) | 2022.09.07 |
2022 ์ธํ์ฝ ํ๊ธฐ (1) | 2022.08.28 |