跳至主内容区

缓存

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

缓存是一种临时存储查询结果或数据的技术,可在后续请求中直接使用,避免每次访问数据库。

TypeORM 内置了缓存支持,您可自定义缓存使用方式。

const users = await userRepository
.createQueryBuilder("user")
.cache(true) // Enable caching
.getMany()

此外,您还能配置缓存持续时间,或使用 Redis 等外部缓存工具提升效率。

const dataSource = new DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
cache: {
type: "redis",
options: {
host: "localhost",
port: 6379,
},
},
})