Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] migrate OkHttp cache to application cache directory #11600

Merged
merged 4 commits into from Jan 13, 2021

Conversation

esamelson
Copy link
Contributor

@esamelson esamelson commented Jan 12, 2021

Why

Follow-up to #11599. Since OTA update assets are stored in a separate location, it's better to keep the OkHttp cache in the application cache directory, rather than the files directory -- this allows the OS to clean up files if it needs to, and reduces the chance that developers' application code will unexpectedly interfere with the OkHttp cache.

How

  • Moved OkHttp cache to Context.getCacheDir, and renamed it to match the name of bare RN OkHttp caches.
  • Added some deletion logic for the old cache in Context.getFilesDir. After deleting we set a key in SharedPreferences so we don't try to delete again.
  • Also increased the maximum cache size to 50Mb -- this is recommended in the OkHttp docs. I checked StorageManager.getCacheQuotaBytes for the cache directory of a fresh installation of the Expo Go client on my Pixel 2, and got 64Mb. Since the cache directory also includes a few other caches (image, picasso, home experience data) 50Mb seems like a reasonable maximum size for the OkHttp cache.
  • Finally, removed some extraneous logic around the OkHttpClient builder that was specifically for ExpoKit apps.

Test Plan

Ran the same tests from #11599 again.

@esamelson esamelson requested a review from ide January 12, 2021 02:09
Base automatically changed from @eric/android-cache-control-1 to master January 12, 2021 23:58
@@ -56,6 +56,7 @@
public static final String SAFE_MANIFEST_KEY = "safe_manifest";
public static final String EXPO_AUTH_SESSION = "expo_auth_session";
public static final String EXPO_AUTH_SESSION_SECRET_KEY = "sessionSecret";
public static final String HAS_CLEARED_LEGACY_OKHTTP_CACHE_KEY = "has_cleared_legacy_okhttp_cache";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit hesitant to put a key called has_cleared_legacy_okhttp_cache on devices with newly installed apps that aren't affected at all by this cache transition. A more forward-looking approach would be to have a key like okhttp_cache_version with the value 1 (no key = we need to try to clear the cache). Then if we were to make cache changes in the future, we could bump this key from 1 to 2.

@esamelson esamelson requested a review from ide January 13, 2021 19:51
@esamelson esamelson merged commit aa910fa into master Jan 13, 2021
@esamelson esamelson deleted the @eric/android-cache-control branch January 13, 2021 21:41
tsapeta pushed a commit that referenced this pull request Jan 16, 2021
# Why

Follow-up to #11599. Since OTA update assets are stored in a separate location, it's better to keep the OkHttp cache in the application cache directory, rather than the files directory -- this allows the OS to clean up files if it needs to, and reduces the chance that developers' application code will unexpectedly interfere with the OkHttp cache.

# How

- Moved OkHttp cache to `Context.getCacheDir`, and renamed it to match the name of bare RN OkHttp caches.
- Added some deletion logic for the old cache in `Context.getFilesDir`. After deleting we set a key in SharedPreferences so we don't try to delete again.
- Also increased the maximum cache size to 50Mb -- this is [recommended in the OkHttp docs](https://square.github.io/okhttp/caching/). I checked [`StorageManager.getCacheQuotaBytes`](https://developer.android.com/reference/android/os/storage/StorageManager#getCacheQuotaBytes(java.util.UUID)) for the cache directory of a fresh installation of the Expo Go client on my Pixel 2, and got 64Mb. Since the cache directory also includes a few other caches (image, picasso, home experience data) 50Mb seems like a reasonable maximum size for the OkHttp cache.
- Finally, removed some extraneous logic around the OkHttpClient builder that was specifically for ExpoKit apps.

# Test Plan

Ran the same tests from #11599 again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants