-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): implement caching of routes
- Loading branch information
Showing
9 changed files
with
83 additions
and
43 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
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,4 @@ | ||
export const titleKey = (titleId: string) => `title:${titleId}`; | ||
export const nameKey = (nameId: string) => `name:${nameId}`; | ||
export const findKey = (query: string) => `find:${query}`; | ||
export const mediaKey = (url: string) => `media:${url}`; |
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,24 @@ | ||
import redis from 'src/utils/redis'; | ||
|
||
const ttl = process.env.REDIS_CACHE_TTL_API ?? 30 * 60; | ||
const redisEnabled = | ||
process.env.USE_REDIS === 'true' || process.env.USE_REDIS_FOR_API_ONLY === 'true'; | ||
|
||
const getOrSetApiCache = async <T extends (...fetcherArgs: any[]) => Promise<any>>( | ||
key: string, | ||
fetcher: T, | ||
...fetcherArgs: Parameters<T> | ||
): Promise<ReturnType<T>> => { | ||
if (!redisEnabled) return await fetcher(...fetcherArgs); | ||
|
||
const dataInCache = await redis.get(key); | ||
if (dataInCache) return JSON.parse(dataInCache); | ||
|
||
const dataToCache = await fetcher(...fetcherArgs); | ||
|
||
await redis.setex(key, ttl, JSON.stringify(dataToCache)); | ||
|
||
return dataToCache; | ||
}; | ||
|
||
export default getOrSetApiCache; |
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