//////
Search
🔳

1번 문제

위에 제시된 첫 번째 문제는 Mongoose의 Query를 건드리면서 해결할 수 있다. mongoosejs.com에 들어가면 queries라는 document를 볼 수 있는데 이걸 참고하면 도움이 많이 된다. (github의 mongoose에서 query.js도 도움이 많이 된다.)
mongoose를 이용한 Query들은 where, sort, select, limit 등 다양한 튜닝이 가능한데, 결국에 이를 실행 시키려면 .exec 을 마지막에 chaining해줘야 한다. 이를 이용해서 반복되는 코드들을 한 번에 정리하는데 도움을 줄 수 있다.
다양한 route path마다 수행해야하는 Query가 있을 것이고, 결국에 해당 Query는 Redis에 있는 데이터인지 확인 후에 데이터가 없으면 .exec()되는 공통적인 특성을 갖는다.
const query = Person .find({ occupation: /host/ }) .where('name.last').equals('Ghost') .where('age').gt(17).lt(66) .where('likes').in(['vaporizing', 'talking']) .limit(10) .sort('-occupation') .select('name occupation'); // Pseudo Code query.exec = function() { // 해당 Query가 이미 실행 됐었는지 확인 후 Redis에서의 Fetch 여부 결정 const result = await client.get('query key'); // 만일 Redis로부터 Fetch를 할 수 있다면 바로 리턴 if(result) { return result; } // Redis로부터 Fetch가 불가능하다면 Query 수행 후 Redis에 저장 const result = await runTheOriginalExecFunction(); client.set('query key', result); return result; } // Below 3 Implementations are as same as each other query.exec((err, result) => console.log(result)); // original query.then(result => console.log(result)); // promisified const result = await query; // async await
JavaScript
복사
첫 번째 문제의 해결 방법은 바로 query.exec이라는 함수를 덮어쓰는 것이다.