-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDD-235]: Redis를 이용한 비디오 URL 해싱 (6h / 5h) (#90)
* chore: redis 사용을 위한 ioredis 의존성 추가 * feat: 싱글톤으로 Redis Client를 생성하는 로직 구현 * feat: 비디오 url 단방향 해시 로직 추가 * feat: redis client를 사용하여 저장하고 삭제하는 메서드 구현 완료 * feat: 비디오 상태 토글 시 redis에 해시값이 알맞게 저장/삭제 되고, 클라이언트에게도 알맞게 해시값/null이 반환되도록 구현 * feat: 비디오 hash로 조회 시 redis에서 조회 후 원본 URL 반환하도록 구현 * refactor: 사용하지 않는 메서드 및 로직 정리 * feat: md5 해싱 시 에러가 발생할 경우를 핸들링하기 위한 try catch 추가 * test: 로직의 변경된 사항들에 맞추어 video controller 단위 테스트 코드 변경 * refactor: redisInstance를 불러오는 로직에서 if문 이중중첩을 방지하기 위해 코드 변경
- Loading branch information
1 parent
e384e08
commit c30e748
Showing
6 changed files
with
204 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Redis from 'ioredis'; | ||
import 'dotenv/config'; | ||
import { | ||
RedisDeleteException, | ||
RedisRetrieveException, | ||
RedisSaveException, | ||
} from 'src/video/exception/video.exception'; | ||
|
||
let redisInstance: Redis; | ||
|
||
function getRedisInstance() { | ||
if (redisInstance) return redisInstance; | ||
|
||
const redisUrl: string = process.env.REDIS_URL as string; | ||
if (redisUrl) { | ||
redisInstance = new Redis(redisUrl); | ||
return redisInstance; | ||
} | ||
|
||
throw new Error('REDIS_URL 환경 변수가 설정되지 않았습니다.'); | ||
} | ||
|
||
export const saveToRedis = async (key: string, value: string) => { | ||
try { | ||
const redis = getRedisInstance(); | ||
await redis.set(key, value); | ||
} catch (error) { | ||
throw new RedisSaveException(); | ||
} | ||
}; | ||
|
||
export const deleteFromRedis = async (key: string) => { | ||
try { | ||
const redis = getRedisInstance(); | ||
await redis.del(key); | ||
} catch (error) { | ||
throw new RedisDeleteException(); | ||
} | ||
}; | ||
|
||
export const getValueFromRedis = async (key: string) => { | ||
try { | ||
const redis = getRedisInstance(); | ||
const value = await redis.get(key); | ||
return value; | ||
} catch (error) { | ||
throw new RedisRetrieveException(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.