-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export genTaskCacheKey from top-level index also
- Loading branch information
Showing
4 changed files
with
30 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { SketchProperties } from "../types/index.js"; | ||
import md5 from "spark-md5"; | ||
import canonicalize from "../util/canonicalize.js"; | ||
|
||
/** | ||
* Generates a cache key for a geoprocessing request, given sketch properties and optional extra parameters (must be JSON compatible object) | ||
* Extra parameters are canonicalized and hashed using md5 to ensure cache key is consistent. Canonicalization ensures object keys are consistent | ||
* but not arrays. If you use arrays as extraParam values, make sure the order stays the same and sort first if needed to generate a consistent cache key. | ||
*/ | ||
export const genTaskCacheKey = ( | ||
/** Properties of sketch to generate cache key for */ | ||
props: SketchProperties, | ||
/** Extra parameters to include in cache key */ | ||
extraParams: Record<string, unknown> = {} | ||
) => { | ||
let cacheKey = `${props.id}-${props.updatedAt}`; | ||
if (Object.keys(extraParams).length > 0) { | ||
// Ensure JSON object has consistent stringification | ||
const canon = canonicalize(extraParams); | ||
// Hash the stringified JSON object | ||
const hash = md5.hash(JSON.stringify(canon)); | ||
// Append the hash to the cache key to keep the key semi-human-readable | ||
cacheKey = `${cacheKey}-${hash}`; | ||
} | ||
return cacheKey; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./genRandomPolygons.js"; | ||
export * from "./genTaskCacheKey.js"; |