-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new Redis caching to queries
- Loading branch information
1 parent
df5e8f7
commit a17b52b
Showing
7 changed files
with
118 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { getConfig } from "@config"; | ||
import type { Prisma } from "@prisma/client"; | ||
import chalk from "chalk"; | ||
import Redis from "ioredis"; | ||
import { createPrismaRedisCache } from "prisma-redis-middleware"; | ||
|
||
const config = getConfig(); | ||
|
||
const cacheRedis = config.redis.cache.enabled | ||
? new Redis({ | ||
host: config.redis.cache.host, | ||
port: Number(config.redis.cache.port), | ||
password: config.redis.cache.password, | ||
db: Number(config.redis.cache.database ?? 0), | ||
}) | ||
: null; | ||
|
||
cacheRedis?.on("error", e => { | ||
console.log(e); | ||
}); | ||
|
||
export { cacheRedis }; | ||
|
||
export const initializeRedisCache = async () => { | ||
if (cacheRedis) { | ||
// Test connection | ||
try { | ||
await cacheRedis.ping(); | ||
} catch (e) { | ||
console.error( | ||
`${chalk.red(`✗`)} ${chalk.bold( | ||
`Error while connecting to Redis` | ||
)}` | ||
); | ||
throw e; | ||
} | ||
|
||
console.log(`${chalk.green(`✓`)} ${chalk.bold(`Connected to Redis`)}`); | ||
|
||
const cacheMiddleware: Prisma.Middleware = createPrismaRedisCache({ | ||
storage: { | ||
type: "redis", | ||
options: { | ||
client: cacheRedis, | ||
invalidation: { | ||
referencesTTL: 300, | ||
}, | ||
}, | ||
}, | ||
cacheTime: 300, | ||
onError: e => { | ||
console.error(e); | ||
}, | ||
}); | ||
|
||
return cacheMiddleware; | ||
} | ||
|
||
return null; | ||
}; |