-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repalce upstash SDK wtih withRedis util and ioredis
- Loading branch information
Showing
5 changed files
with
261 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,42 @@ | ||
import { Redis } from "@upstash/redis" | ||
import type { ParsedFile, TreeItem } from "./github" | ||
|
||
const db = Redis.fromEnv() | ||
import { withRedis } from "./redis.server" | ||
|
||
export async function getTreeCache(repo: string, sha: string) { | ||
return await db.get(`tree:${repo}:${sha}`) as TreeItem[] | ||
return withRedis<TreeItem[]>(async (db) => { | ||
const data = await db.get(`tree:${repo}:${sha}`) | ||
return data && JSON.parse(data) | ||
}) | ||
} | ||
|
||
export async function setTreeCache(repo: string, sha: string, tree: TreeItem[]) { | ||
await db.set(`tree:${repo}:${sha}`, tree, { | ||
ex: 60 * 60 * 24 // 1 day | ||
return withRedis(async (db) => { | ||
const oneDay = 60 * 60 * 24 | ||
await db.setex(`tree:${repo}:${sha}`, oneDay, JSON.stringify(tree)) | ||
}) | ||
} | ||
|
||
export async function deleteTreeCache(repo: string, sha: string) { | ||
await db.del(`tree:${repo}:${sha}`) | ||
return withRedis(async (db) => { | ||
await db.del(`tree:${repo}:${sha}`) | ||
}) | ||
} | ||
|
||
export async function getFileCache(repo: string, branch: string, path: string) { | ||
return await db.get(`file:${repo}/${branch}/${path}`) as ParsedFile | ||
return withRedis<ParsedFile>(async (db) => { | ||
const data = await db.get(`file:${repo}/${branch}/${path}`) | ||
return data && JSON.parse(data) | ||
}) | ||
} | ||
|
||
export async function setFileCache(repo: string, branch: string, path: string, file: ParsedFile) { | ||
await db.set(`file:${repo}/${branch}/${path}`, file, { | ||
ex: 60 * 60 * 24 // 1 day | ||
return withRedis(async (db) => { | ||
const oneDay = 60 * 60 * 24 | ||
await db.setex(`file:${repo}/${branch}/${path}`, oneDay, JSON.stringify(file)) | ||
}) | ||
} | ||
|
||
export async function deleteFileCache(repo: string, branch: string, path: string) { | ||
await db.del(`file:${repo}/${branch}/${path}`) | ||
return withRedis(async (db) => { | ||
await db.del(`file:${repo}/${branch}/${path}`) | ||
}) | ||
} |
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,22 @@ | ||
import { Redis } from 'ioredis' | ||
|
||
const REDIS_URL = process.env.REDIS_URL | ||
|
||
/** | ||
* Create a redis instance only for the duration of the function call | ||
*/ | ||
export async function withRedis<T>(fn: (redis: Redis) => Promise<T>) { | ||
if (!REDIS_URL) { | ||
throw new Error('REDIS_URL not found in process.env') | ||
} | ||
const redis = new Redis( | ||
REDIS_URL, | ||
{ family: 6, reconnectOnError: () => 1 } | ||
) | ||
redis.on('error', err => { | ||
console.error('Redis error: ', err) | ||
}) | ||
const result = await fn(redis) | ||
await redis.quit() | ||
return result | ||
} |
Oops, something went wrong.