Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions parse/src/main/java/com/parse/Parse.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ static void initialize(Configuration configuration, ParsePlugins parsePlugins) {
if (configuration.localDataStoreEnabled) {
offlineStore = new OfflineStore(configuration.context);
} else {
ParseKeyValueCache.maxKeyValueCacheBytes = configuration.maxKeyValueCacheBytes;
ParseKeyValueCache.maxKeyValueCacheFiles = configuration.maxKeyValueCacheFiles;
ParseKeyValueCache.initialize(configuration.context);
}

Expand Down Expand Up @@ -582,6 +584,8 @@ public static final class Configuration {
final boolean allowCustomObjectId;
final OkHttpClient.Builder clientBuilder;
final int maxRetries;
final int maxKeyValueCacheBytes;
final int maxKeyValueCacheFiles;

private Configuration(Builder builder) {
this.context = builder.context;
Expand All @@ -592,10 +596,19 @@ private Configuration(Builder builder) {
this.allowCustomObjectId = builder.allowCustomObjectId;
this.clientBuilder = builder.clientBuilder;
this.maxRetries = builder.maxRetries;
this.maxKeyValueCacheBytes = builder.maxKeyValueCacheBytes;
this.maxKeyValueCacheFiles = builder.maxKeyValueCacheFiles;
}

/** Allows for simple constructing of a {@code Configuration} object. */
public static final class Builder {
/** The default maximum number of bytes to use for the Parse cache on disk. */
public static final int DEFAULT_MAX_KEY_VALUE_CACHE_BYTES =
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES;
/** The default maximum number of files to store in the Parse cache on disk. */
public static final int DEFAULT_MAX_KEY_VALUE_CACHE_FILES =
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES;

private final Context context;
private String applicationId;
private String clientKey;
Expand All @@ -604,6 +617,8 @@ public static final class Builder {
private boolean allowCustomObjectId;
private OkHttpClient.Builder clientBuilder;
private int maxRetries = DEFAULT_MAX_RETRIES;
private int maxKeyValueCacheBytes = DEFAULT_MAX_KEY_VALUE_CACHE_BYTES;
private int maxKeyValueCacheFiles = DEFAULT_MAX_KEY_VALUE_CACHE_FILES;

/**
* Initialize a bulider with a given context.
Expand Down Expand Up @@ -707,6 +722,36 @@ public Builder maxRetries(int maxRetries) {
return this;
}

/**
* Set the maximum amount of bytes to use for the Parse cache on disk. Defaults to
* {@link Builder#DEFAULT_MAX_KEY_VALUE_CACHE_BYTES}.
*
* @param maxKeyValueCacheBytes The maximum number of bytes to use for the cache.
* @return The same builder, for easy chaining.
*/
public Builder maxKeyValueCacheBytes(int maxKeyValueCacheBytes) {
if (maxKeyValueCacheBytes < 0) {
throw new IllegalArgumentException("maxKeyValueCacheBytes must be >= 0");
}
this.maxKeyValueCacheBytes = maxKeyValueCacheBytes;
return this;
}

/**
* Set the maximum number of files to store in the Parse cache on disk. Defaults to
* {@link Builder#DEFAULT_MAX_KEY_VALUE_CACHE_FILES}.
*
* @param maxKeyValueCacheFiles The maximum number of files to store in the cache.
* @return The same builder, for easy chaining.
*/
public Builder maxKeyValueCacheFiles(int maxKeyValueCacheFiles) {
if (maxKeyValueCacheFiles < 0) {
throw new IllegalArgumentException("maxKeyValueCacheFiles must be >= 0");
}
this.maxKeyValueCacheFiles = maxKeyValueCacheFiles;
return this;
}

/**
* Construct this builder into a concrete {@code Configuration} instance.
*
Expand Down
60 changes: 60 additions & 0 deletions parse/src/test/java/com/parse/ParseKeyValueCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,64 @@ public void testGetSizeWithoutCacheDir() {
// Verify size is zero
assertEquals(0, ParseKeyValueCache.size());
}

@Test
public void testDefaultCacheConfiguration() {
assertEquals(
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES,
ParseKeyValueCache.maxKeyValueCacheBytes);
assertEquals(
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES,
ParseKeyValueCache.maxKeyValueCacheFiles);
assertEquals(
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES,
Parse.Configuration.Builder.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES);
assertEquals(
ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES,
Parse.Configuration.Builder.DEFAULT_MAX_KEY_VALUE_CACHE_FILES);
}

@Test
public void testCustomCacheSize() {
int customBytes = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES + 1024;
int customFiles = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES + 100;

ParseKeyValueCache.maxKeyValueCacheBytes = customBytes;
ParseKeyValueCache.maxKeyValueCacheFiles = customFiles;

assertEquals(customBytes, ParseKeyValueCache.maxKeyValueCacheBytes);
assertEquals(customFiles, ParseKeyValueCache.maxKeyValueCacheFiles);
}

@Test
public void testConfigurationBuilderCacheSize() {
int customBytes = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_BYTES + 1024;
int customFiles = ParseKeyValueCache.DEFAULT_MAX_KEY_VALUE_CACHE_FILES + 100;

Parse.Configuration configuration =
new Parse.Configuration.Builder(null)
.applicationId("test")
.maxKeyValueCacheBytes(customBytes)
.maxKeyValueCacheFiles(customFiles)
.build();

assertEquals(customBytes, configuration.maxKeyValueCacheBytes);
assertEquals(customFiles, configuration.maxKeyValueCacheFiles);
}

@Test(expected = IllegalArgumentException.class)
public void testConfigurationBuilderRejectsNegativeCacheBytes() {
new Parse.Configuration.Builder(null)
.applicationId("test")
.maxKeyValueCacheBytes(-1)
.build();
}

@Test(expected = IllegalArgumentException.class)
public void testConfigurationBuilderRejectsNegativeCacheFiles() {
new Parse.Configuration.Builder(null)
.applicationId("test")
.maxKeyValueCacheFiles(-1)
.build();
}
}