# 将数据加载到缓存中

Alluxio 提供了两种将数据加载到其缓存中的主要方法：首次读取时被动缓存和主动预加载。了解这些方法有助于您优化数据访问模式并确保应用程序的高性能。

## 被动缓存：首次读取时缓存

被动缓存是数据进入 Alluxio 缓存的默认且最常见的方式。该过程简单且自动：

1. 应用程序请求通过 Alluxio 读取文件。
2. Alluxio 检查该文件的数据是否已在其缓存中。
3. 如果数据未被缓存（“缓存未命中”），Alluxio 会从底层文件系统 (UFS) 中检索数据。
4. 当数据从 UFS 流式传输到应用程序时，Alluxio 会同时将其写入指定工作节点上的缓存中。

后续对同一文件的读取将直接从 Alluxio 缓存中以内存速度提供，从而避免了访问速度较慢的 UFS。这种“读取时缓存”行为无需特殊配置即可开箱即用。

## 主动缓存：预加载数据

在某些情况下，您可能希望在应用程序需要数据*之前*将数据加载到缓存中。此过程称为预加载或缓存预热，非常适用于初始缓存未命中延迟不可接受的性能关键型工作负载。例如，您可以在开始机器学习训练作业之前预加载大型数据集，以确保训练过程从一开始就以最大速度运行。

Alluxio 的分布式加载功能使您可以有效地将数据从 UFS 加载到 Alluxio 集群中。加载操作分布在所有工作节点上，以最大限度地提高并行性和速度。对于高并发场景，分布式加载还可以利用[文件分段](/ee-ai-cn/ai-3.7/data-access/performance/file-reading.md#segmenting-large-files-across-multiple-workers)和[复制](/ee-ai-cn/ai-3.7/data-access/high-availability/multiple-replicas.md)来进一步优化集群中的数据分布和可用性。

### 使用 `job load` 命令

触​​发分布式加载的最常见方法是通过 `job load` 命令行界面 (CLI)。CLI 向 Alluxio Coordinator发送请求，然后Coordinator在工作节点之间协调加载操作。

```shell
bin/alluxio job load [flags] <path>
```

**示例：**

要将 UFS 中 `/data` 目录的内容加载到 Alluxio 中：

```shell
$ bin/alluxio job load /data
Progress for loading path '/data':
        Settings:       bandwidth: unlimited    verify: false
        Job State: SUCCEEDED
        Files Processed: 1000
        Bytes Loaded: 125.00MB
        Throughput: 2509.80KB/s
        Block load failure rate: 0.00%
        Files Failed: 0
```

有关标志和选项的完整列表，请参阅 `job load` [CLI 文档](/ee-ai-cn/ai-3.7/reference/user-cli.md#job-load)。

### 使用 REST API

您还可以通过 REST API 以编程方式启动分布式加载。这对于将缓存预加载集成到自动化工作流和数据管道中非常有用。

有关使用 REST 端点进行分布式加载的更多详细信息，请参阅 [API 参考](/ee-ai-cn/ai-3.7/reference/rest-api.md) 页面。

> 注意：历史加载任务列表会保留一段可配置的时间。默认情况下，仅显示过去七天的任务。可以使用 `alluxio.job.retention.time` 属性调整此保留时间。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.alluxio.io/ee-ai-cn/ai-3.7/cache/loading-data-into-the-cache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
