缓存
缓存是把查询结果或数据临时存储起来,供后续请求使用,而不必每次都查询数据库的技术。
TypeORM 内置了缓存支持,你可以自定义缓存的使用方式。
const users = await userRepository
.createQueryBuilder("user")
.cache(true) // 启用缓存
.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,
},
},
})Caching is the technique of temporarily storing query results or data for use in future requests without querying the database each time.
TypeORM has built-in caching support, and you can customize how caching is used.
const users = await userRepository
.createQueryBuilder("user")
.cache(true) // Enable caching
.getMany()Additionally, you can configure cache duration or use external caching tools like Redis for better efficiency.
const dataSource = new DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
cache: {
type: "redis",
options: {
host: "localhost",
port: 6379,
},
},
})