-
-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
6 changed files
with
136 additions
and
24 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
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
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,81 @@ | ||
import { Redis } from '@upstash/redis' | ||
|
||
import { safeJsonParse } from './helper' | ||
|
||
const UPSTASH_TOKEN = process.env.UPSTASH_TOKEN | ||
const UPSTASH_URL = process.env.UPSTASH_URL | ||
|
||
let redis: Redis | ||
const getRedis = () => { | ||
if (redis) { | ||
return redis | ||
} | ||
if (!UPSTASH_TOKEN || !UPSTASH_URL) { | ||
return null | ||
} | ||
const _redis = new Redis({ | ||
url: UPSTASH_URL, | ||
token: UPSTASH_TOKEN, | ||
}) | ||
|
||
redis = _redis | ||
return _redis | ||
} | ||
|
||
export const setCache = async (key: string, value: string, ttl: number) => { | ||
const _redis = getRedis() | ||
if (!_redis) { | ||
return | ||
} | ||
await _redis | ||
.set(key, value, { | ||
ex: ttl, | ||
}) | ||
.catch((err) => { | ||
console.error('setCache', err) | ||
}) | ||
} | ||
|
||
export const getCache = async (key: string) => { | ||
const _redis = getRedis() | ||
if (!_redis) { | ||
return null | ||
} | ||
return await _redis.get(key).catch((err) => { | ||
console.error('getCache', err) | ||
|
||
return null | ||
}) | ||
} | ||
|
||
export const getOrSetCache = async <T>( | ||
key: string, | ||
setFn: () => Promise<T>, | ||
ttl: number, | ||
): Promise<T> => { | ||
const cache = await getCache(key) | ||
|
||
if (cache) { | ||
if (typeof cache === 'string') { | ||
const tryParse = safeJsonParse(cache as any) | ||
if (tryParse) { | ||
return tryParse as T | ||
} | ||
} else { | ||
return cache as T | ||
} | ||
} | ||
|
||
const fallbackData = await setFn() | ||
|
||
await setCache(key, JSON.stringify(fallbackData), ttl) | ||
return fallbackData | ||
} | ||
|
||
export const onlyGetOrSetCacheInVercelButFallback: typeof getOrSetCache = | ||
async (key, setFn, ttl) => { | ||
if (process.env.VERCEL || process.env.VERCEL_ENV) { | ||
return getOrSetCache(key, setFn, ttl) | ||
} | ||
return setFn() | ||
} |