Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis typescript 전환 #497

Merged
merged 3 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/http-errors": "^1.8.2",
"@types/ioredis": "^5.0.0",
"@types/jsonwebtoken": "^8.5.8",
"@types/lodash": "^4.14.180",
"@types/mocha": "^9.1.0",
Expand Down
56 changes: 29 additions & 27 deletions src/controllers/Perfume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ const searchPerfume: RequestHandler = (
* - application/json
* parameters:
* - in: body
* name: body
* name: body
* schema:
* type: object
* type: object
* responses:
* 200:
* description: 성공
Expand All @@ -266,34 +266,31 @@ const updateSimilarPerfumes: RequestHandler = async (
next: NextFunction
): Promise<any> => {
try {
const perfumeSimilarRequest : any = req.body;
const perfumeSimilarRequest: any = req.body;

logger.debug(
`${LOG_TAG} updateSimilarPerfumes(
body = ${JSON.stringify(req.body)}
)`
);

const result: any = await Perfume.updateSimilarPerfumes(perfumeSimilarRequest);


const result = await Perfume.updateSimilarPerfumes(
perfumeSimilarRequest
);

LoggerHelper.logTruncated(
logger.debug,
`${LOG_TAG} updateSimilarPerfumes's result = ${result}`
);

res.status(StatusCode.OK).json(
new SimpleResponseDTO(
MSG_POST_PERFUME_RECOMMEND_SIMMILAR_SUCCESS
)
new SimpleResponseDTO(MSG_POST_PERFUME_RECOMMEND_SIMMILAR_SUCCESS)
);
} catch(err: any) {
} catch (err: any) {
next(err);
}


}
};


/**
* @swagger
* /perfume/{perfumeIdx}/like:
Expand Down Expand Up @@ -506,7 +503,7 @@ const recommendPersonalPerfume: RequestHandler = (
)})`
);
const pagingDTO: PagingDTO = pagingRequestDTO.toPageDTO();
return Perfume.getPerfumesByRandom(pagingDTO.limit, 3)
return Perfume.getPerfumesByRandom(pagingDTO.limit, 3)
.then((result: ListAndCountDTO<PerfumeThumbKeywordDTO>) => {
return supplementPerfumeWithRandom(result, pagingDTO.limit);
})
Expand Down Expand Up @@ -582,12 +579,10 @@ const recommendSimilarPerfumeList: RequestHandler = async (
next: NextFunction
): Promise<any> => {
try {
const pagingRequestDTO: PagingRequestDTO = PagingRequestDTO.createByJson(
req.query,
{
const pagingRequestDTO: PagingRequestDTO =
PagingRequestDTO.createByJson(req.query, {
requestSize: DEFAULT_SIMILAR_PERFUMES_REQUEST_SIZE,
}
);
});
logger.debug(
`${LOG_TAG} recommendSimilarPerfumeList(query = ${JSON.stringify(
req.query
Expand All @@ -596,23 +591,30 @@ const recommendSimilarPerfumeList: RequestHandler = async (
const pagingDTO: PagingDTO = pagingRequestDTO.toPageDTO();
const perfumeIdx: number = req.params['perfumeIdx'];

const similarPerfumeList: ListAndCountDTO<PerfumeThumbKeywordDTO> = await Perfume.getRecommendedSimilarPerfumeList(perfumeIdx, pagingDTO.limit)
const response: ListAndCountDTO<PerfumeRecommendResponse> = similarPerfumeList.convertType(PerfumeRecommendResponse.createByJson);

const similarPerfumeList: ListAndCountDTO<PerfumeThumbKeywordDTO> =
await Perfume.getRecommendedSimilarPerfumeList(
perfumeIdx,
pagingDTO.limit
);
const response: ListAndCountDTO<PerfumeRecommendResponse> =
similarPerfumeList.convertType(
PerfumeRecommendResponse.createByJson
);

LoggerHelper.logTruncated(
logger.debug,
`${LOG_TAG} recommendPersonalPerfume's result = ${response}`
);

res.status(StatusCode.OK).json(
new ResponseDTO<ListAndCountDTO<PerfumeRecommendResponse>>(
MSG_GET_RECOMMEND_SIMILAR_PERFUMES,
response
)
);
} catch(err: any) {
} catch (err: any) {
next(err);
}
}
};

/**
Expand Down
30 changes: 12 additions & 18 deletions src/dao/PerfumeDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const PERFUME_THUMB_COLUMNS: string[] = [
'updatedAt',
];

import redis from '@utils/db/redis';

const SQL_SEARCH_PERFUME_SELECT: string =
'SELECT ' +
'p.perfume_idx AS perfumeIdx, p.brand_idx AS brandIdx, p.name, p.english_name AS englishName, p.image_url AS imageUrl, p.created_at AS createdAt, p.updated_at AS updatedAt, ' +
Expand Down Expand Up @@ -391,23 +393,17 @@ class PerfumeDao {
*/
async updateSimilarPerfumes(similarPerfumes: {
[x: number]: number[];
}): Promise<[err: number, result: number][]> {
try {
const redis = require('@utils/db/redis.js');

const obj = similarPerfumes;
const multi = await redis.multi();
}): Promise<[error: Error | null, result: unknown][] | null> {
const obj = similarPerfumes;
const multi = await redis.multi();

for (const key in obj) {
multi.del(`recs.perfume:${key}`);
obj[key].forEach((v: any) => {
multi.rpush(`recs.perfume:${key}`, v);
});
}
return await multi.exec();
} catch (err) {
throw err;
for (const key in obj) {
multi.del(`recs.perfume:${key}`);
obj[key].forEach((v: any) => {
multi.rpush(`recs.perfume:${key}`, v);
});
}
return await multi.exec();
}
/**
* 서베이 추천 향수 조회
Expand Down Expand Up @@ -468,9 +464,7 @@ class PerfumeDao {
`${LOG_TAG} getSimilarPerfumeIdxList(perfumeIdx = ${perfumeIdx}, size = ${size})`
);

const client = require('@utils/db/redis.js');

const result = await client.lrange(
const result = await redis.lrange(
`recs.perfume:${perfumeIdx}`,
0,
size - 1
Expand Down
23 changes: 0 additions & 23 deletions src/utils/db/redis.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/utils/db/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import properties from '@properties';
import Redis, { RedisOptions } from 'ioredis';

const config: RedisOptions = {
host: properties.REDIS_HOST,
port: Number(properties.REDIS_PORT),
db: Number(properties.REDIS_DB_ID),
};

const redisClient = new Redis(config);

export default redisClient;
9 changes: 4 additions & 5 deletions tests/test_unit/dao/PerfumeDao.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,10 @@ describe('# perfumeDao Test', () => {
describe('# recommend similar Test', () => {
it('# update recommended similar perfumes ', async () => {
try {
const result: any[] =
await perfumeDao.updateSimilarPerfumes({
1: [2, 3],
2: [3, 4, 5],
});
const result: any = await perfumeDao.updateSimilarPerfumes({
1: [2, 3],
2: [3, 4, 5],
});

expect(result.length).to.be.eq(7);

Expand Down