Skip to content

Commit 950bdbc

Browse files
committed
Add maximumCachedObjectLength to limit the cached entry size
1 parent 4183489 commit 950bdbc

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The AWS S3 build cache implementation has a few configuration options:
4747
| `region` | The AWS region the S3 bucket is located in. | yes | |
4848
| `bucket` | The name of the AWS S3 bucket where cache objects should be stored. | yes | |
4949
| `path` | The path under which all cache objects should be stored. | no | |
50+
| `maximumCachedObjectLength` | Maximum object size that can be stored and retrieved from the cache | no | 50'000'000 |
5051
| `reducedRedundancy` | Whether or not to use [reduced redundancy](https://aws.amazon.com/s3/reduced-redundancy/). | no | true |
5152
| `endpoint` | Alternative S3 compatible endpoint | no | |
5253
| `headers` | A map with HTTP headers to be added to each request (nulls are ignored). e.g. `[ 'x-header-name': 'header-value' ]` | no | |
@@ -61,9 +62,9 @@ The `buildCache` configuration block might look like this:
6162

6263
```
6364
apply plugin: 'ch.myniva.s3-build-cache'
64-
65+
6566
ext.isCiServer = System.getenv().containsKey("CI")
66-
67+
6768
buildCache {
6869
local {
6970
enabled = !isCiServer
@@ -74,7 +75,6 @@ The `buildCache` configuration block might look like this:
7475
push = isCiServer
7576
}
7677
}
77-
7878
```
7979

8080
More details about configuring the Gradle build cache can be found in the

src/main/kotlin/ch/myniva/gradle/caching/s3/AwsS3BuildCache.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ open class AwsS3BuildCache : AbstractBuildCache() {
2121
var region: String? = null
2222
var bucket: String? = null
2323
var path: String? = null
24+
var maximumCachedObjectLength: Long = 50 * 1024 * 1024
2425
var isReducedRedundancy = true
2526
var endpoint: String? = null
2627
var headers: Map<String?, String?>? = null

src/main/kotlin/ch/myniva/gradle/caching/s3/internal/AwsS3BuildCacheService.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class AwsS3BuildCacheService internal constructor(
3030
private val s3: AmazonS3,
3131
private val bucketName: String,
3232
private val path: String?,
33-
private val reducedRedundancy: Boolean
33+
private val reducedRedundancy: Boolean,
34+
private val maximumCachedObjectLength: Long
3435
) : BuildCacheService {
3536
companion object {
3637
private const val BUILD_CACHE_CONTENT_TYPE = "application/vnd.gradle.build-cache-artifact"
@@ -49,6 +50,16 @@ class AwsS3BuildCacheService internal constructor(
4950
val bucketPath = key.getBucketPath()
5051
try {
5152
s3.getObject(bucketName, bucketPath).use { s3Object ->
53+
if (s3Object.objectMetadata.contentLength > maximumCachedObjectLength) {
54+
logger.info(
55+
"Cache item '{}' '{}' in S3 bucket size is {}, and it exceeds maximumCachedObjectLength {}. Will skip the retrieval",
56+
key.displayName,
57+
bucketPath,
58+
s3Object.objectMetadata.contentLength,
59+
maximumCachedObjectLength
60+
)
61+
return false
62+
}
5263
reader.readFrom(s3Object.objectContent)
5364
}
5465
return true
@@ -76,6 +87,17 @@ class AwsS3BuildCacheService internal constructor(
7687

7788
override fun store(key: BuildCacheKey, writer: BuildCacheEntryWriter) {
7889
val bucketPath = key.getBucketPath()
90+
val itemSize = writer.size
91+
if (itemSize > maximumCachedObjectLength) {
92+
logger.info(
93+
"Cache item '{}' '{}' in S3 bucket size is {}, and it exceeds maximumCachedObjectLength {}. Will skip caching it.",
94+
key.displayName,
95+
bucketPath,
96+
itemSize,
97+
maximumCachedObjectLength
98+
)
99+
return
100+
}
79101
logger.info("Start storing cache entry '{}' in S3 bucket", bucketPath)
80102
val meta = ObjectMetadata().apply {
81103
contentType = BUILD_CACHE_CONTENT_TYPE

src/main/kotlin/ch/myniva/gradle/caching/s3/internal/AwsS3BuildCacheServiceFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class AwsS3BuildCacheServiceFactory : BuildCacheServiceFactory<AwsS3BuildCache>
5050
createS3Client(config),
5151
config.bucket!!,
5252
config.path,
53-
config.isReducedRedundancy
53+
config.isReducedRedundancy,
54+
config.maximumCachedObjectLength
5455
)
5556
}
5657

0 commit comments

Comments
 (0)