Skip to content

Commit

Permalink
export genTaskCacheKey from top-level index also
Browse files Browse the repository at this point in the history
  • Loading branch information
twelch committed May 17, 2024
1 parent 4ae974d commit deeeb7a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
31 changes: 1 addition & 30 deletions packages/geoprocessing/src/clients/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import { GeoprocessingTask } from "../aws/tasks.js";
import {
GeoprocessingRequest,
GeoprocessingRequestParams,
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: GeoprocessingRequestParams = {}
) => {
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;
};
import { GeoprocessingRequest } from "../types/index.js";

/**
* Runs task by sending GET request to url with payload and optional flags
Expand Down
3 changes: 2 additions & 1 deletion packages/geoprocessing/src/hooks/useFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
GeoprocessingProject,
GeoprocessingRequestParams,
} from "../types/index.js";
import { runTask, finishTask, genTaskCacheKey } from "../clients/tasks.js";
import { runTask, finishTask } from "../clients/tasks.js";
import { genTaskCacheKey } from "../util/genTaskCacheKey.js";
import cloneDeep from "lodash/cloneDeep.js";

interface PendingRequest {
Expand Down
26 changes: 26 additions & 0 deletions packages/geoprocessing/src/util/genTaskCacheKey.ts
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;
};
1 change: 1 addition & 0 deletions packages/geoprocessing/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./genRandomPolygons.js";
export * from "./genTaskCacheKey.js";

0 comments on commit deeeb7a

Please sign in to comment.