# BucketCacheOptions

- Source: [`aws-codebuild/lib/cache.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-codebuild/lib/cache.ts#L6)
- Type last changed: v2.205.0 · 15 Jul 2025

## Example

TypeScript:

```ts
declare const sourceBucket: s3.Bucket;
declare const myCachingBucket: s3.Bucket;

new codebuild.Project(this, 'ProjectA', {
  source: codebuild.Source.s3({
    bucket: sourceBucket,
    path: 'path/to/source-a.zip',
  }),
  // configure the same bucket and path prefix
  cache: codebuild.Cache.bucket(myCachingBucket, {
    prefix: 'cache',
    // use the same cache namespace
    cacheNamespace: 'cache-namespace',
  }),
  buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    phases: {
      build: {
        commands: ['...'],
      },
    },
    // specify the same cache key and paths
    cache: {
      key: 'unique-key',
      paths: [
        '/root/cachedir/**/*',
      ],
    },
  }),
});

new codebuild.Project(this, 'ProjectB', {
  source: codebuild.Source.s3({
    bucket: sourceBucket,
    path: 'path/to/source-b.zip',
  }),
  // configure the same bucket and path prefix
  cache: codebuild.Cache.bucket(myCachingBucket, {
    prefix: 'cache',
    // use the same cache namespace
    cacheNamespace: 'cache-namespace',
  }),
  buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    phases: {
      build: {
        commands: ['...'],
      },
    },
    // specify the same cache key and paths
    cache: {
      key: 'unique-key',
      paths: [
        '/root/cachedir/**/*',
      ],
    },
  }),
});
```

Python:

```python
# source_bucket: s3.Bucket
# my_caching_bucket: s3.Bucket

codebuild.Project(self, "ProjectA",
    source=codebuild.Source.s3(
        bucket=source_bucket,
        path="path/to/source-a.zip"
    ),
    # configure the same bucket and path prefix
    cache=codebuild.Cache.bucket(my_caching_bucket,
        prefix="cache",
        # use the same cache namespace
        cache_namespace="cache-namespace"
    ),
    build_spec=codebuild.BuildSpec.from_object({
        "version": "0.2",
        "phases": {
            "build": {
                "commands": ["..."]
            }
        },
        # specify the same cache key and paths
        "cache": {
            "key": "unique-key",
            "paths": ["/root/cachedir/**/*"
            ]
        }
    })
)

codebuild.Project(self, "ProjectB",
    source=codebuild.Source.s3(
        bucket=source_bucket,
        path="path/to/source-b.zip"
    ),
    # configure the same bucket and path prefix
    cache=codebuild.Cache.bucket(my_caching_bucket,
        prefix="cache",
        # use the same cache namespace
        cache_namespace="cache-namespace"
    ),
    build_spec=codebuild.BuildSpec.from_object({
        "version": "0.2",
        "phases": {
            "build": {
                "commands": ["..."]
            }
        },
        # specify the same cache key and paths
        "cache": {
            "key": "unique-key",
            "paths": ["/root/cachedir/**/*"
            ]
        }
    })
)
```

Java:

```java
Bucket sourceBucket;
Bucket myCachingBucket;

Project.Builder.create(this, "ProjectA")
        .source(Source.s3(S3SourceProps.builder()
                .bucket(sourceBucket)
                .path("path/to/source-a.zip")
                .build()))
        // configure the same bucket and path prefix
        .cache(Cache.bucket(myCachingBucket, BucketCacheOptions.builder()
                .prefix("cache")
                // use the same cache namespace
                .cacheNamespace("cache-namespace")
                .build()))
        .buildSpec(BuildSpec.fromObject(Map.of(
                "version", "0.2",
                "phases", Map.of(
                        "build", Map.of(
                                "commands", List.of("..."))),
                // specify the same cache key and paths
                "cache", Map.of(
                        "key", "unique-key",
                        "paths", List.of("/root/cachedir/**/*")))))
        .build();

Project.Builder.create(this, "ProjectB")
        .source(Source.s3(S3SourceProps.builder()
                .bucket(sourceBucket)
                .path("path/to/source-b.zip")
                .build()))
        // configure the same bucket and path prefix
        .cache(Cache.bucket(myCachingBucket, BucketCacheOptions.builder()
                .prefix("cache")
                // use the same cache namespace
                .cacheNamespace("cache-namespace")
                .build()))
        .buildSpec(BuildSpec.fromObject(Map.of(
                "version", "0.2",
                "phases", Map.of(
                        "build", Map.of(
                                "commands", List.of("..."))),
                // specify the same cache key and paths
                "cache", Map.of(
                        "key", "unique-key",
                        "paths", List.of("/root/cachedir/**/*")))))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Bucket sourceBucket;
Bucket myCachingBucket;

new Project(this, "ProjectA", new ProjectProps {
    Source = Source.S3(new S3SourceProps {
        Bucket = sourceBucket,
        Path = "path/to/source-a.zip"
    }),
    // configure the same bucket and path prefix
    Cache = Cache.Bucket(myCachingBucket, new BucketCacheOptions {
        Prefix = "cache",
        // use the same cache namespace
        CacheNamespace = "cache-namespace"
    }),
    BuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
        { "version", "0.2" },
        { "phases", new Dictionary<string, IDictionary<string, string[]>> {
            { "build", new Struct {
                Commands = new [] { "..." }
            } }
        } },
        // specify the same cache key and paths
        { "cache", new Dictionary<string, object> {
            { "key", "unique-key" },
            { "paths", new [] { "/root/cachedir/**/*" } }
        } }
    })
});

new Project(this, "ProjectB", new ProjectProps {
    Source = Source.S3(new S3SourceProps {
        Bucket = sourceBucket,
        Path = "path/to/source-b.zip"
    }),
    // configure the same bucket and path prefix
    Cache = Cache.Bucket(myCachingBucket, new BucketCacheOptions {
        Prefix = "cache",
        // use the same cache namespace
        CacheNamespace = "cache-namespace"
    }),
    BuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
        { "version", "0.2" },
        { "phases", new Dictionary<string, IDictionary<string, string[]>> {
            { "build", new Struct {
                Commands = new [] { "..." }
            } }
        } },
        // specify the same cache key and paths
        { "cache", new Dictionary<string, object> {
            { "key", "unique-key" },
            { "paths", new [] { "/root/cachedir/**/*" } }
        } }
    })
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var sourceBucket Bucket
var myCachingBucket Bucket

codebuild.NewProject(this, jsii.String("ProjectA"), &ProjectProps{
	Source: codebuild.Source_S3(&S3SourceProps{
		Bucket: sourceBucket,
		Path: jsii.String("path/to/source-a.zip"),
	}),
	// configure the same bucket and path prefix
	Cache: codebuild.Cache_Bucket(myCachingBucket, &BucketCacheOptions{
		Prefix: jsii.String("cache"),
		// use the same cache namespace
		CacheNamespace: jsii.String("cache-namespace"),
	}),
	BuildSpec: codebuild.BuildSpec_FromObject(map[string]interface{}{
		"version": jsii.String("0.2"),
		"phases": map[string]map[string][]*string{
			"build": map[string][]*string{
				"commands": []*string{
					jsii.String("..."),
				},
			},
		},
		// specify the same cache key and paths
		"cache": map[string]interface{}{
			"key": jsii.String("unique-key"),
			"paths": []*string{
				jsii.String("/root/cachedir/**/*"),
			},
		},
	}),
})

codebuild.NewProject(this, jsii.String("ProjectB"), &ProjectProps{
	Source: codebuild.Source_*S3(&S3SourceProps{
		Bucket: sourceBucket,
		Path: jsii.String("path/to/source-b.zip"),
	}),
	// configure the same bucket and path prefix
	Cache: codebuild.Cache_*Bucket(myCachingBucket, &BucketCacheOptions{
		Prefix: jsii.String("cache"),
		// use the same cache namespace
		CacheNamespace: jsii.String("cache-namespace"),
	}),
	BuildSpec: codebuild.BuildSpec_*FromObject(map[string]interface{}{
		"version": jsii.String("0.2"),
		"phases": map[string]map[string][]*string{
			"build": map[string][]*string{
				"commands": []*string{
					jsii.String("..."),
				},
			},
		},
		// specify the same cache key and paths
		"cache": map[string]interface{}{
			"key": jsii.String("unique-key"),
			"paths": []*string{
				jsii.String("/root/cachedir/**/*"),
			},
		},
	}),
})
```

## Properties

- `cacheNamespace`: string — Defines the scope of the cache.

  **Default:** undefined - No cache namespace, which means that the cache is not shared across multiple projects.

  You can use this namespace to share a cache across multiple projects.

- `prefix`: string — The prefix to use to store the cache in the bucket.
