Why Volley DiskBasedCache splicing without direct access to the cache file name
Asked Answered
P

1

3
/**
 * Creates a pseudo-unique filename for the specified cache key.
 *
 * @param key The key to generate a file name for.
 * @return A pseudo-unique filename.
 */
private String getFilenameForKey(String key) {
    int firstHalfLength = key.length() / 2;
    String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode());
    localFilename += String.valueOf(key.substring(firstHalfLength).hashCode());
    return localFilename;
}

This code from Google Volley DiskBasedCache.
Why splicing without direct access.
e.g:

return String.valueOf(key.hashCode());
Pondweed answered 25/1, 2016 at 2:40 Comment(0)
S
1

I'm not one of the developers, but I believe their train of thought was this: Our keys are URLs. A lot of the times, different URLs (typically of the same site) share a good number of characters. That's why the key hashing is performed on the first half of the key and on the second half separately - to create more variance in the file names. Hash isn't super reliable in Java.

Spa answered 25/1, 2016 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.