Skip to content

Commit a2a855b

Browse files
runningcodeclaude
andcommitted
ref(core): Inject the cache LazyDirectory via the constructor (JAVA-613)
Have the composition roots (EnvelopeCache.create and AndroidEnvelopeCache) build the LazyDirectory and pass it into CacheStrategy, so the cache no longer constructs its own directory collaborator from a path string. The public String constructor is kept and delegates, preserving binary compatibility. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1fdc454 commit a2a855b

5 files changed

Lines changed: 17 additions & 9 deletions

File tree

sentry-android-core/src/main/java/io/sentry/android/core/cache/AndroidEnvelopeCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public AndroidEnvelopeCache(final @NotNull SentryAndroidOptions options) {
4848
final @NotNull ICurrentDateProvider currentDateProvider) {
4949
super(
5050
options,
51-
Objects.requireNonNull(options.getCacheDirPath(), "cacheDirPath must not be null"),
51+
new LazyDirectory(
52+
Objects.requireNonNull(options.getCacheDirPath(), "cacheDirPath must not be null")),
5253
options.getMaxCacheItems());
5354
this.currentDateProvider = currentDateProvider;
5455
}

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4836,6 +4836,7 @@ public class io/sentry/cache/EnvelopeCache : io/sentry/cache/IEnvelopeCache {
48364836
protected static final field UTF_8 Ljava/nio/charset/Charset;
48374837
protected final field cacheLock Lio/sentry/util/AutoClosableReentrantLock;
48384838
protected final field sessionLock Lio/sentry/util/AutoClosableReentrantLock;
4839+
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/util/LazyDirectory;I)V
48394840
public fun <init> (Lio/sentry/SentryOptions;Ljava/lang/String;I)V
48404841
public static fun create (Lio/sentry/SentryOptions;)Lio/sentry/cache/IEnvelopeCache;
48414842
public fun discard (Lio/sentry/SentryEnvelope;)V

sentry/src/main/java/io/sentry/cache/CacheStrategy.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ abstract class CacheStrategy {
4545

4646
CacheStrategy(
4747
final @NotNull SentryOptions options,
48-
final @NotNull String directoryPath,
48+
final @NotNull LazyDirectory directory,
4949
final int maxSize) {
50-
Objects.requireNonNull(directoryPath, "Directory is required.");
5150
this.options = Objects.requireNonNull(options, "SentryOptions is required.");
52-
53-
this.directory = new LazyDirectory(directoryPath);
54-
51+
this.directory = Objects.requireNonNull(directory, "Directory is required.");
5552
this.maxSize = maxSize;
5653
}
5754

sentry/src/main/java/io/sentry/cache/EnvelopeCache.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.sentry.transport.NoOpEnvelopeCache;
2727
import io.sentry.util.AutoClosableReentrantLock;
2828
import io.sentry.util.HintUtils;
29+
import io.sentry.util.LazyDirectory;
2930
import io.sentry.util.Objects;
3031
import java.io.BufferedInputStream;
3132
import java.io.BufferedReader;
@@ -83,15 +84,22 @@ public class EnvelopeCache extends CacheStrategy implements IEnvelopeCache {
8384
options.getLogger().log(WARNING, "cacheDirPath is null, returning NoOpEnvelopeCache");
8485
return NoOpEnvelopeCache.getInstance();
8586
} else {
86-
return new EnvelopeCache(options, cacheDirPath, maxCacheItems);
87+
return new EnvelopeCache(options, new LazyDirectory(cacheDirPath), maxCacheItems);
8788
}
8889
}
8990

9091
public EnvelopeCache(
9192
final @NotNull SentryOptions options,
9293
final @NotNull String cacheDirPath,
9394
final int maxCacheItems) {
94-
super(options, cacheDirPath, maxCacheItems);
95+
this(options, new LazyDirectory(cacheDirPath), maxCacheItems);
96+
}
97+
98+
public EnvelopeCache(
99+
final @NotNull SentryOptions options,
100+
final @NotNull LazyDirectory directory,
101+
final int maxCacheItems) {
102+
super(options, directory, maxCacheItems);
95103
previousSessionLatch = new CountDownLatch(1);
96104
}
97105

sentry/src/test/java/io/sentry/cache/CacheStrategyTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.sentry.Session
99
import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport
1010
import io.sentry.clientreport.DiscardReason
1111
import io.sentry.clientreport.DiscardedEvent
12+
import io.sentry.util.LazyDirectory
1213
import java.io.ByteArrayInputStream
1314
import java.io.File
1415
import java.io.InputStreamReader
@@ -130,7 +131,7 @@ class CacheStrategyTest {
130131
}
131132

132133
private class CustomCache(options: SentryOptions, path: String, maxSize: Int) :
133-
CacheStrategy(options, path, maxSize)
134+
CacheStrategy(options, LazyDirectory(path), maxSize)
134135

135136
private fun createTempFilesSortByOldestToNewest(): Array<File> {
136137
val f1 = Files.createTempFile(fixture.dir.toPath(), "f1", ".json").toFile()

0 commit comments

Comments
 (0)