Re-initialize the cache if the directory was deleted.#297
Merged
jpd236 merged 3 commits intogoogle:masterfrom Oct 3, 2019
Merged
Re-initialize the cache if the directory was deleted.#297jpd236 merged 3 commits intogoogle:masterfrom
jpd236 merged 3 commits intogoogle:masterfrom
Conversation
If the user clears the cache from the Android Settings page, the DiskBasedCache's root directory is deleted. This means that the app will be running without a cache until it is restarted and the cache initialized. This fix initializes the cache when it finds that the root directory no longer exists. Note that the first entry after deletion that is put into the cache is still lost, the cache is only re-initialized when putting that entry fails. Adding retries would be more complicated, since we would have to avoid getting into a loop if creating the root directory fails.
jpd236
reviewed
Oct 2, 2019
| @Test | ||
| public void initializeIfRootDirectoryDeleted() { | ||
| temporaryFolder.delete(); | ||
| DiskBasedCache uninitializedCache = new DiskBasedCache(temporaryFolder.getRoot(), MAX_SIZE); |
Collaborator
There was a problem hiding this comment.
nit - in the spirit of testing more representative behavior, should this unit test perhaps create the cache, call initialize(), and only then delete the temporary folder?
The behavior tested here isn't a supported sequence in general - we don't expect the cache to work if you don't initialize it. It happens to hit the code path you want, but that's not necessarily reliable. On the other hand, we know user devices hit the scenario that an already-initialized cache has the directory deleted from underneath it.
Rather than testing with an uninitialized test, the test should use an initialized cache, delete the root directory, and then confirm that the cache is re-initialized.
joebowbeer
reviewed
Oct 2, 2019
| @@ -258,10 +258,11 @@ public synchronized void put(String key, Entry entry) { | |||
| pruneIfNeeded(); | |||
| return; | |||
Contributor
There was a problem hiding this comment.
Suggested change
| return; |
If move logic inside the catch block, then remove the return that is no longer needed.
After the other code was moved into the catch statement, this return is no longer needed.
Collaborator
|
Thanks for the change and the review! |
HeterPu
pushed a commit
to HeterPu/volley
that referenced
this pull request
Oct 7, 2019
Re-initialize the cache if the directory was deleted. (google#297)
HeterPu
added a commit
to HeterPu/volley
that referenced
this pull request
Oct 7, 2019
This was referenced Mar 9, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If the user clears the cache from the Android Settings page, the
DiskBasedCache's root directory is deleted. This means that the app will
be running without a cache until it is restarted and the cache
initialized.
This fix initializes the cache when it finds that the root directory no
longer exists.
Note that the first entry after deletion that is put into the cache is
still lost, the cache is only re-initialized when putting that entry
fails. Adding retries would be more complicated, since we would have to
avoid getting into a loop if creating the root directory fails.