---
slug: /quickstart/635927/caching
displayed_sidebar: "quickstart"
hide_table_of_contents: true
title: "Use caching"
---

# Quickstart

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import QuickstartDoc from "@site/src/components/quickstartDoc.js"
import Embed from "@site/src/components/embed.js"

export const ids = {
    Go: "1J5Fs4NoQcW",
    "Node.js": "HjFX8td15TX",
    Python: "TCdOYrKMfV_d"
}

<QuickstartDoc embeds={ids}>

## Use caching

One of Dagger's most powerful features is its ability to cache data across pipeline runs. This is typically most useful when dealing with package managers such as `npm`, `maven`, `pip` and similar. For these tools to cache properly, they need their own cache data (usually a directory) to be persisted between runs.

Dagger lets you define one or more such directories as cache volumes and persist their contents across runs. This enables you to reuse the contents of the cache volume(s) every time the pipeline runs, and thereby speed up pipeline operations.

:::note
Cache volumes are not meant to be shared outside of Dagger; they are for persisting specific parts of the internal state of your pipeline, for optimal use of your tool's native caching features.
:::

You may have noticed that the example pipeline executes the `npm install` command to download the application's dependencies every time the pipeline runs. Since these dependencies are usually locked to specific versions in the application's manifest, re-downloading them on every pipeline run is inefficient and time-consuming.

This step is, therefore, a good candidate for a cache volume. Let's update the pipeline accordingly.

:::tip
The `npm install` command is appropriate for a React application, but other applications are likely to use different commands. Modify your Dagger pipeline accordingly.
:::

<Tabs groupId="language" className="embeds">
<TabItem value="Go">

<Embed id="1J5Fs4NoQcW" />

This revised pipeline now uses a cache volume for the application dependencies.

- It uses the client's `CacheVolume()` method to initialize a new cache volume.
- It uses the `Container.WithMountedCache()` method to mount this cache volume at the `node_modules/` mount point in the container.
- It uses the `Container.WithExec()` method to define the `npm install` command. When executed, this command downloads and installs dependencies in the `node_modules/` directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.

Run the pipeline by executing the command below from the application directory:

```shell
dagger run go run ci/main.go
```

</TabItem>
<TabItem value="Node.js">

<Embed id="HjFX8td15TX" />

This revised pipeline now uses a cache volume for the application dependencies.

- It uses the client's `cacheVolume()` method to initialize a new cache volume.
- It uses the `Container.withMountedCache()` method to mount this cache volume at the `node_modules/` mount point in the container.
- It uses the `Container.withExec()` method to define the `npm install` command. When executed, this command downloads and installs dependencies in the `node_modules/` directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.

Run the pipeline by executing the command below from the application directory:

```shell
dagger run node ci/index.mjs
```

</TabItem>
<TabItem value="Python">

<Embed id="TCdOYrKMfV_d" />

This revised pipeline now uses a cache volume for the application dependencies.

- It uses the client's `cache_volume()` method to initialize a new cache volume.
- It uses the `Container.with_mounted_cache()` method to mount this cache volume at the `node_modules/` mount point in the container.
- It uses the `Container.with_exec()` method to define the `npm install` command. When executed, this command downloads and installs dependencies in the `node_modules/` directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.

Run the pipeline by executing the command below from the application directory:

```shell
dagger run python ci/main.py
```

</TabItem>
</Tabs>

This revised pipeline produces the same result as before.

Run the pipeline a few times. Notice that on the first run, the application dependencies are downloaded as usual. However, since the dependencies are cached, subsequent pipeline runs will skip the download operation and be significantly faster (assuming that there are no other changes to the application code).

:::note
In addition to cache volumes, Dagger has a separate cache for pipeline operations. Changes in cache volumes or secrets do not invalidate the Dagger pipeline operations cache.
:::

</QuickstartDoc>
