forked from woowacourse/perf-basecamp
-
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.
feat: trending API 호출 결과를 캐싱하기 위한 캐시 모듈 구현
- Loading branch information
Showing
4 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
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
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,30 @@ | ||
interface CacheItem<T> { | ||
data: T; | ||
expiredTime: number; | ||
} | ||
|
||
class Cache { | ||
private cache: Record<string, CacheItem<any>> = {}; | ||
|
||
isValidCache(key: string): boolean { | ||
const item = this.cache[key]; | ||
if (!item) return false; | ||
|
||
return Date.now() <= item.expiredTime; | ||
} | ||
|
||
get<T>(key: string): T | null { | ||
if (!this.cache[key]) return null; | ||
|
||
return this.cache[key].data; | ||
} | ||
|
||
set<T>(key: string, data: T, ttl: number): void { | ||
const expiredTime = Date.now() + ttl; | ||
this.cache[key] = { data, expiredTime }; | ||
} | ||
} | ||
|
||
const cache = new Cache(); | ||
|
||
export default cache; |