24
24
/** Settings used to configure a FirebaseFirestore instance. */
25
25
@ PublicApi
26
26
public final class FirebaseFirestoreSettings {
27
+ /**
28
+ * Constant to use with {@link FirebaseFirestoreSettings.Builder#setCacheSizeBytes(long)} to
29
+ * disable garbage collection.
30
+ */
31
+ @ PublicApi public static final long CACHE_SIZE_UNLIMITED = -1 ;
32
+
33
+ private static final long MINIMUM_CACHE_BYTES = 1 * 1024 * 1024 ; // 1 MB
34
+ // TODO(b/121269744): Set this to be the default value after SDK is past version 1.0
35
+ // private static final long DEFAULT_CACHE_SIZE_BYTES = 100 * 1024 * 1024; // 100 MB
36
+ // For now, we are rolling this out with collection disabled. Once the SDK has hit version 1.0,
37
+ // we will switch the default to the above value, 100 MB.
38
+ private static final long DEFAULT_CACHE_SIZE_BYTES = CACHE_SIZE_UNLIMITED ;
27
39
private static final String DEFAULT_HOST = "firestore.googleapis.com" ;
28
40
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = false ;
29
41
@@ -34,6 +46,7 @@ public static final class Builder {
34
46
private boolean sslEnabled ;
35
47
private boolean persistenceEnabled ;
36
48
private boolean timestampsInSnapshotsEnabled ;
49
+ private long cacheSizeBytes ;
37
50
38
51
/** Constructs a new FirebaseFirestoreSettings Builder object. */
39
52
@ PublicApi
@@ -42,6 +55,7 @@ public Builder() {
42
55
sslEnabled = true ;
43
56
persistenceEnabled = true ;
44
57
timestampsInSnapshotsEnabled = DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED ;
58
+ cacheSizeBytes = DEFAULT_CACHE_SIZE_BYTES ;
45
59
}
46
60
47
61
/**
@@ -124,6 +138,30 @@ public Builder setTimestampsInSnapshotsEnabled(boolean value) {
124
138
return this ;
125
139
}
126
140
141
+ /**
142
+ * Sets an approximate cache size threshold for the on-disk data. If the cache grows beyond this
143
+ * size, Firestore will start removing data that hasn't been recently used. The size is not a
144
+ * guarantee that the cache will stay below that size, only that if the cache exceeds the given
145
+ * size, cleanup will be attempted.
146
+ *
147
+ * <p>By default, collection is disabled (the value is set to {@link
148
+ * FirebaseFirestoreSettings#CACHE_SIZE_UNLIMITED}). In a future release, collection will be
149
+ * enabled by default, with a default cache size of 100 MB. The minimum value is 1 MB.
150
+ *
151
+ * @return A settings object on which the cache size is configured as specified by the given
152
+ * {@code value}.
153
+ */
154
+ @ NonNull
155
+ @ PublicApi
156
+ public Builder setCacheSizeBytes (long value ) {
157
+ if (value != CACHE_SIZE_UNLIMITED && value < MINIMUM_CACHE_BYTES ) {
158
+ throw new IllegalArgumentException (
159
+ "Cache size must be set to at least " + MINIMUM_CACHE_BYTES + " bytes" );
160
+ }
161
+ this .cacheSizeBytes = value ;
162
+ return this ;
163
+ }
164
+
127
165
@ NonNull
128
166
@ PublicApi
129
167
public FirebaseFirestoreSettings build () {
@@ -139,13 +177,15 @@ public FirebaseFirestoreSettings build() {
139
177
private final boolean sslEnabled ;
140
178
private final boolean persistenceEnabled ;
141
179
private final boolean timestampsInSnapshotsEnabled ;
180
+ private final long cacheSizeBytes ;
142
181
143
182
/** Constructs a FirebaseFirestoreSettings object based on the values in the Builder. */
144
183
private FirebaseFirestoreSettings (Builder builder ) {
145
184
host = builder .host ;
146
185
sslEnabled = builder .sslEnabled ;
147
186
persistenceEnabled = builder .persistenceEnabled ;
148
187
timestampsInSnapshotsEnabled = builder .timestampsInSnapshotsEnabled ;
188
+ cacheSizeBytes = builder .cacheSizeBytes ;
149
189
}
150
190
151
191
@ Override
@@ -161,7 +201,8 @@ public boolean equals(@Nullable Object o) {
161
201
return host .equals (that .host )
162
202
&& sslEnabled == that .sslEnabled
163
203
&& persistenceEnabled == that .persistenceEnabled
164
- && timestampsInSnapshotsEnabled == that .timestampsInSnapshotsEnabled ;
204
+ && timestampsInSnapshotsEnabled == that .timestampsInSnapshotsEnabled
205
+ && cacheSizeBytes == that .cacheSizeBytes ;
165
206
}
166
207
167
208
@ Override
@@ -170,6 +211,7 @@ public int hashCode() {
170
211
result = 31 * result + (sslEnabled ? 1 : 0 );
171
212
result = 31 * result + (persistenceEnabled ? 1 : 0 );
172
213
result = 31 * result + (timestampsInSnapshotsEnabled ? 1 : 0 );
214
+ result = 31 * result + (int ) cacheSizeBytes ;
173
215
return result ;
174
216
}
175
217
@@ -211,4 +253,13 @@ public boolean isPersistenceEnabled() {
211
253
public boolean areTimestampsInSnapshotsEnabled () {
212
254
return timestampsInSnapshotsEnabled ;
213
255
}
256
+
257
+ /**
258
+ * Returns the threshold for the cache size above which the SDK will attempt to collect the least
259
+ * recently used documents.
260
+ */
261
+ @ PublicApi
262
+ public long getCacheSizeBytes () {
263
+ return cacheSizeBytes ;
264
+ }
214
265
}
0 commit comments