# 缓存

> 英文标题：Caching
> 官网原文：https://typeorm.io/docs/performance-optimization/caching
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 6 块

---

<!-- EN -->
# Caching

<!-- ZH -->
# 缓存

<!-- EN -->
Caching is the technique of temporarily storing query results or data for use in future requests without querying the database each time.

<!-- ZH -->
缓存是把查询结果或数据临时存储起来，供后续请求使用，而不必每次都查询数据库的技术。

<!-- EN -->
TypeORM has built-in caching support, and you can customize how caching is used.

<!-- ZH -->
TypeORM 内置了缓存支持，你可以自定义缓存的使用方式。

<!-- EN -->
```typescript
const users = await userRepository
    .createQueryBuilder("user")
    .cache(true) // Enable caching
    .getMany()
```

<!-- ZH -->
```typescript
const users = await userRepository
    .createQueryBuilder("user")
    .cache(true) // 启用缓存
    .getMany()
```

<!-- EN -->
Additionally, you can configure cache duration or use external caching tools like Redis for better efficiency.

<!-- ZH -->
此外，你还可以配置缓存时长，或使用 Redis 这类外部缓存工具以获得更好的效率。

<!-- EN -->
```typescript
const dataSource = new DataSource({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test",
    cache: {
        type: "redis",
        options: {
            host: "localhost",
            port: 6379,
        },
    },
})
```

<!-- ZH -->
```typescript
const dataSource = new DataSource({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test",
    cache: {
        type: "redis",
        options: {
            host: "localhost",
            port: 6379,
        },
    },
})
```
