Skip to content

Commit c19f02f

Browse files
authored
feat: Add maxKeyValueCacheBytes and maxKeyValueCacheFiles to configuration to manage cache size (#1227)
1 parent f27a8fb commit c19f02f

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

parse/src/main/java/com/parse/Parse.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ static void initialize(Configuration configuration, ParsePlugins parsePlugins) {
171171
if (configuration.localDataStoreEnabled) {
172172
offlineStore = new OfflineStore(configuration.context);
173173
} else {
174+
ParseKeyValueCache.maxKeyValueCacheBytes = configuration.maxKeyValueCacheBytes;
175+
ParseKeyValueCache.maxKeyValueCacheFiles = configuration.maxKeyValueCacheFiles;
174176
ParseKeyValueCache.initialize(configuration.context);
175177
}
176178

@@ -582,6 +584,8 @@ public static final class Configuration {
582584
final boolean allowCustomObjectId;
583585
final OkHttpClient.Builder clientBuilder;
584586
final int maxRetries;
587+
final int maxKeyValueCacheBytes;
588+
final int maxKeyValueCacheFiles;
585589

586590
private Configuration(Builder builder) {
587591
this.context = builder.context;
@@ -592,10 +596,19 @@ private Configuration(Builder builder) {
592596
this.allowCustomObjectId = builder.allowCustomObjectId;
593597
this.clientBuilder = builder.clientBuilder;
594598
this.maxRetries = builder.maxRetries;
599+
this.maxKeyValueCacheBytes = builder.maxKeyValueCacheBytes;
600+
this.maxKeyValueCacheFiles = builder.maxKeyValueCacheFiles;
595601
}
596602

597603
/** Allows for simple constructing of a {@code Configuration} object. */
598604
public static final class Builder {
605+
/** The default maximum number of bytes to use for the Parse cache on disk. */
606+
public static final int DEFAULT_MAX_KEY_VALUE_CACHE_BYTES =
607+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES;
608+
/** The default maximum number of files to store in the Parse cache on disk. */
609+
public static final int DEFAULT_MAX_KEY_VALUE_CACHE_FILES =
610+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES;
611+
599612
private final Context context;
600613
private String applicationId;
601614
private String clientKey;
@@ -604,6 +617,8 @@ public static final class Builder {
604617
private boolean allowCustomObjectId;
605618
private OkHttpClient.Builder clientBuilder;
606619
private int maxRetries = DEFAULT_MAX_RETRIES;
620+
private int maxKeyValueCacheBytes = DEFAULT_MAX_KEY_VALUE_CACHE_BYTES;
621+
private int maxKeyValueCacheFiles = DEFAULT_MAX_KEY_VALUE_CACHE_FILES;
607622

608623
/**
609624
* Initialize a bulider with a given context.
@@ -707,6 +722,36 @@ public Builder maxRetries(int maxRetries) {
707722
return this;
708723
}
709724

725+
/**
726+
* Set the maximum amount of bytes to use for the Parse cache on disk. Defaults to
727+
* {@link Builder#DEFAULT_MAX_KEY_VALUE_CACHE_BYTES}.
728+
*
729+
* @param maxKeyValueCacheBytes The maximum number of bytes to use for the cache.
730+
* @return The same builder, for easy chaining.
731+
*/
732+
public Builder maxKeyValueCacheBytes(int maxKeyValueCacheBytes) {
733+
if (maxKeyValueCacheBytes < 0) {
734+
throw new IllegalArgumentException("maxKeyValueCacheBytes must be >= 0");
735+
}
736+
this.maxKeyValueCacheBytes = maxKeyValueCacheBytes;
737+
return this;
738+
}
739+
740+
/**
741+
* Set the maximum number of files to store in the Parse cache on disk. Defaults to
742+
* {@link Builder#DEFAULT_MAX_KEY_VALUE_CACHE_FILES}.
743+
*
744+
* @param maxKeyValueCacheFiles The maximum number of files to store in the cache.
745+
* @return The same builder, for easy chaining.
746+
*/
747+
public Builder maxKeyValueCacheFiles(int maxKeyValueCacheFiles) {
748+
if (maxKeyValueCacheFiles < 0) {
749+
throw new IllegalArgumentException("maxKeyValueCacheFiles must be >= 0");
750+
}
751+
this.maxKeyValueCacheFiles = maxKeyValueCacheFiles;
752+
return this;
753+
}
754+
710755
/**
711756
* Construct this builder into a concrete {@code Configuration} instance.
712757
*

parse/src/test/java/com/parse/ParseKeyValueCacheTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,64 @@ public void testGetSizeWithoutCacheDir() {
9393
// Verify size is zero
9494
assertEquals(0, ParseKeyValueCache.size());
9595
}
96+
97+
@Test
98+
public void testDefaultCacheConfiguration() {
99+
assertEquals(
100+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES,
101+
ParseKeyValueCache.maxKeyValueCacheBytes);
102+
assertEquals(
103+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES,
104+
ParseKeyValueCache.maxKeyValueCacheFiles);
105+
assertEquals(
106+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES,
107+
Parse.Configuration.Builder.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES);
108+
assertEquals(
109+
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES,
110+
Parse.Configuration.Builder.DEFAULT_MAX_KEY_VALUE_CACHE_FILES);
111+
}
112+
113+
@Test
114+
public void testCustomCacheSize() {
115+
int customBytes = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES + 1024;
116+
int customFiles = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES + 100;
117+
118+
ParseKeyValueCache.maxKeyValueCacheBytes = customBytes;
119+
ParseKeyValueCache.maxKeyValueCacheFiles = customFiles;
120+
121+
assertEquals(customBytes, ParseKeyValueCache.maxKeyValueCacheBytes);
122+
assertEquals(customFiles, ParseKeyValueCache.maxKeyValueCacheFiles);
123+
}
124+
125+
@Test
126+
public void testConfigurationBuilderCacheSize() {
127+
int customBytes = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES + 1024;
128+
int customFiles = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES + 100;
129+
130+
Parse.Configuration configuration =
131+
new Parse.Configuration.Builder(null)
132+
.applicationId("test")
133+
.maxKeyValueCacheBytes(customBytes)
134+
.maxKeyValueCacheFiles(customFiles)
135+
.build();
136+
137+
assertEquals(customBytes, configuration.maxKeyValueCacheBytes);
138+
assertEquals(customFiles, configuration.maxKeyValueCacheFiles);
139+
}
140+
141+
@Test(expected = IllegalArgumentException.class)
142+
public void testConfigurationBuilderRejectsNegativeCacheBytes() {
143+
new Parse.Configuration.Builder(null)
144+
.applicationId("test")
145+
.maxKeyValueCacheBytes(-1)
146+
.build();
147+
}
148+
149+
@Test(expected = IllegalArgumentException.class)
150+
public void testConfigurationBuilderRejectsNegativeCacheFiles() {
151+
new Parse.Configuration.Builder(null)
152+
.applicationId("test")
153+
.maxKeyValueCacheFiles(-1)
154+
.build();
155+
}
96156
}

0 commit comments

Comments
 (0)