Skip to content

Commit

Permalink
feat(cache): generate consistent cache keys based on input
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Oct 11, 2022
1 parent 7fe55ef commit f05714a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/ttl-cache/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function hash(input: string): string {
if (typeof input !== "string") {
input = JSON.stringify(input);
}

const numberHash = input.split("").reduce((a, b) => {
a = (a << 5) - a + a * 24 + b.charCodeAt(0);
a |= 0;
return a;
}, 0);

return numberHash.toString(32);
}

0 comments on commit f05714a

Please sign in to comment.