| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
- docker compose example
- Raspberry Pi docker install
- JavaScript
- cloudflare ssl
- 코어자바스크립트 정리
- Raspberry Pi docker-compose install
- 코어 자바스크립트
- nodejs myslq2
- docker image 배포
- Dockerfile setting
- XMLHttpRequest 예제
- cloudflare DNS
- docker image deploy
- docker compose
- XMLHttpRequest example
- docker
- nodejs DB
- XMLHttpRequest with promise
- cloudflare certbot
- nodejs mariaDB
- dockerignore example
- 코어자바스크립트
- dockerignore setting
- docker compose setting
- cloudflare Origin Server
- 로스트아크 open API
- cloudflare Origin Server CA
- 로스트아크 API
- cloudflare
- Dockerfile example
- Today
- Total
오늘
API 호출을 줄이기 위한 데이터 캐싱 본문
현재 로스트아크에서 제공하는 오픈 API은 발급받은 Key마다 사용횟수 제한이 있다. (기본 : 100회/1분)
사용자가 요청을 할 때마다 API를 호출하게 된다면 사용횟수 제한에 걸려서 서비스가 정상적으로 동작할 수 없다.
아래 시퀀스의 하늘색 박스부분의 구현을 통해서 문제를 해결하기로 했다.
구현 내용 : 오픈API 호출이 정상적으로 이루어 지면 해당 데이터를 저장하고, 업데이트 시간을 기록.
사용자의 요청 시간 - 데이터 업데이트 시간이 일정수치 이상일때, 오픈API 호출 다시 데이터 저장 반복.
++ 예정 : cluster-shared-memory 패키지 적용 후 공유메모리에 데이터 저장.
현재는 클러스터별(4개)로 각각 메모리에 저장되기 때문에 불필요한 API 사용이 남아있음.
- myloa 서비스의 기본적인 시퀀스
( Search DB 로 작성하기는 했지만, 그냥 메모리에 저장을하는 데이터 관리를 위해 MemoryCache 클래스 사용. )

코드.
// getData를 할 때에 데이터가 유효하지 않으면, 바인딩한 function을 통해 데이터를 새로 업데이트 함.
class MemoryCache {
constructor(len, validTime) {
this.dataList = [];
this.validTime = validTime;
this.name = '';
this.createDataList(len);
}
createDataList(len) {
for(let i = 0; i < len; i++)
this.dataList.push({updated: null, data: null, func: null});
}
isValidData(idx) {
const now = new Date();
return ( this.dataList[idx].updated && ((this.dataList[idx].updated.getTime() + this.validTime) > now.getTime()));
}
reset(idx) {
try {
if( 0 <= idx && idx < this.dataList.length )
{
this.dataList[idx].updated = null;
this.dataList[idx].data = null;
}
} catch (error) {
logger.log('error', `MemoryCache(${this.name}) reset(${idx}) error : ${error}`);
}
}
setDataFunc(idx, func) {
try {
if( 0 <= idx && idx < this.dataList.length )
this.dataList[idx].func = func;
} catch (error) {
logger.log('error', `MemoryCache(${this.name}) setDataFunc(${idx}) error : ${error}`);
}
}
async getData(idx) {
try {
if( 0 <= idx && idx < this.dataList.length )
{
if(!this.isValidData(idx) && this.dataList[idx].func)
{
this.dataList[idx].updated = new Date();
this.dataList[idx].data = await this.dataList[idx].func();
}
return this.dataList[idx].data;
}
} catch (error) {
logger.log('error', `MemoryCache(${this.name}) getData(${idx}) error : ${error}`);
}
return null;
}
}
// 실제 사용 사례
// MemoryCache 를 상속받아서 로스트아크 오픈 API의 News 항목의 데이터를 관리할 클래스 새로 생성.
class NewsCache extends MemoryCache {
constructor() {
super(5, 1000*60*60);
this.name = 'NewsCache';
super.setDataFunc(NewsCache.events, lostarkAPI.wrapper.requestEvents.bind(null));
super.setDataFunc(NewsCache.notices_notification, lostarkAPI.wrapper.requestNotices.bind(null, '공지'));
super.setDataFunc(NewsCache.notices_inspection, lostarkAPI.wrapper.requestNotices.bind(null, '점검'));
super.setDataFunc(NewsCache.notices_shop, lostarkAPI.wrapper.requestNotices.bind(null, '상점'));
super.setDataFunc(NewsCache.notices_event, lostarkAPI.wrapper.requestNotices.bind(null, '이벤트'));
}
static events = 0;
static notices_notification = 1;
static notices_inspection = 2;
static notices_shop = 3;
static notices_event = 4;
// 클라이언트 요청이 문자열로 전달되기 때문에 문자열 > idx 값으로 변환하기 위한 Object
static notices = {
'공지': NewsCache.notices_notification,
'점검': NewsCache.notices_inspection,
'상점': NewsCache.notices_shop,
'이벤트': NewsCache.notices_event,
};
}
const newsCache = new NewsCache();
module.exports = {
getEvents: async () => {
return await newsCache.getData(NewsCache.events);
},
getNotices: async (type) => {
return await newsCache.getData(NewsCache.notices[type]);
},
}
+ 로스트아크 오픈 API 가이드
https://developer-lostark.game.onstove.com/getting-started#API-NEWS
Lostark OpenAPI Developer Portal
GETTING STARTED You can get started using Lostark Open API following basic procedures below and understand how it works. Login Before you hit any of Lostark Open APIs, you need to sign in to your Stove.com account. If you don't have one yet, please sign up
developer-lostark.game.onstove.com
'myloa' 카테고리의 다른 글
| myloa docker compose setting (0) | 2023.05.14 |
|---|---|
| myloa - 로스트아크 오픈 API를 활용한 토이 프로젝트 (0) | 2023.03.06 |