Why Volley DiskBasedCache splicing without direct

2020-03-26 06:07发布

/**
 * 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());

1条回答
Evening l夕情丶
2楼-- · 2020-03-26 06:44

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.

查看更多
登录 后发表回答