Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Created January 4, 2020 07:19
Show Gist options
  • Save julianrubisch/5abbd50b87de21a92c6792e743fbd052 to your computer and use it in GitHub Desktop.
Save julianrubisch/5abbd50b87de21a92c6792e743fbd052 to your computer and use it in GitHub Desktop.
makeAssetCachePath v1
/* from https://github.com/segment-boneyard/hash-mod/blob/master/lib/index.js */
function integerHash(string) {
return (string + "").split("").reduce((memo, item) => {
return (memo * 31 * item.charCodeAt(0)) % 982451653;
}, 7);
}
function makeAssetCachePath(cacheDir, cacheKey) {
const hash = crypto
.createHash("sha256")
.update(cacheKey)
.digest("hex");
const quotient = Math.floor(integerHash(hash) / 0x1000);
const bucket1 = integerHash(hash) % 0x1000;
const bucket2 = quotient % 0x1000;
const bucket1HexString = sprintf("%x", bucket1);
const bucket2HexString = sprintf("%x", bucket2);
return {
dir1: bucket1HexString,
dir2: bucket2HexString,
path: path.join(
cacheDir,
bucket1HexString,
bucket2HexString,
hash.toString()
)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment