Skip to content

Commit 0bb76d3

Browse files
authored
Cache FID to avoid multiple disk reads. (#1571)
* Cache FID to avoid multiple disk reads. * Addressing Rayo's comments.
1 parent b26d198 commit 0bb76d3

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
6767
private final Object lock = new Object();
6868
private final ExecutorService backgroundExecutor;
6969
private final ExecutorService networkExecutor;
70+
/* FID of this Firebase Installations instance. Cached after successfully registering and
71+
persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/
72+
private String cachedFid = null;
7073

7174
@GuardedBy("lock")
7275
private final List<StateListener> listeners = new ArrayList<>();
@@ -284,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
284287
}
285288

286289
private String doGetId() {
290+
if (cachedFid != null) {
291+
return cachedFid;
292+
}
287293
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
288294
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
289295
// i.e networkExecutor
@@ -337,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) {
337343
// Store the prefs to persist the result of the previous step.
338344
insertOrUpdatePrefs(prefs);
339345

346+
// Update cachedFID, if FID is successfully REGISTERED and persisted.
347+
if (prefs.isRegistered()) {
348+
cachedFid = prefs.getFirebaseInstallationId();
349+
}
350+
340351
// Let the caller know about the result.
341352
if (prefs.isErrored()) {
342353
triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG));
@@ -493,6 +504,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
493504
case AUTH_ERROR:
494505
// The the server refused to generate a new auth token due to bad credentials, clear the
495506
// FID to force the generation of a new one.
507+
cachedFid = null;
496508
return prefs.withNoGeneratedFid();
497509
default:
498510
throw new IOException();
@@ -504,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
504516
* storage.
505517
*/
506518
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException {
519+
cachedFid = null;
507520
PersistedInstallationEntry entry = getMultiProcessSafePrefs();
508521
if (entry.isRegistered()) {
509522
// Call the FIS servers to delete this Firebase Installation Id.
@@ -519,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
519532
"Failed to delete a Firebase Installation.", Status.BAD_CONFIG);
520533
}
521534
}
522-
523535
insertOrUpdatePrefs(entry.withNoGeneratedFid());
524536
return null;
525537
}

0 commit comments

Comments
 (0)