Skip to content

Commit

Permalink
handle delimiter in cache key segment
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Sep 5, 2024
1 parent 6eed49d commit 9a72403
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/util-endpoints/src/cache/EndpointCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class EndpointCache {
private parameters: string[] = [];

/**
* @param [size] - desired average maximum capacity. A buffer of 10 additional keys will be allowed
* before keys are dropped.
* @param [params] - list of params to consider as part of the cache key.
*
* If the params list is not populated, all object keys will be considered.
Expand All @@ -30,6 +32,10 @@ export class EndpointCache {
*/
public get(endpointParams: EndpointParams, resolver: () => EndpointV2): EndpointV2 {
const key = this.hash(endpointParams);
if (key === false) {
return resolver();
}

if (!this.data.has(key)) {
if (this.data.size > this.capacity + 10) {
const keys = this.data.keys();
Expand All @@ -51,11 +57,15 @@ export class EndpointCache {
return this.data.size;
}

private hash(endpointParams: EndpointParams): string {
private hash(endpointParams: EndpointParams): string | false {
let buffer = "";
const params = this.parameters.length ? this.parameters : Object.keys(endpointParams);
for (const param of params) {
buffer += endpointParams[param] ?? "" + "|";
const val = String(endpointParams[param] ?? "");
if (val.includes("|;")) {
return false;
}
buffer += val + "|;";
}
return buffer;
}
Expand Down

0 comments on commit 9a72403

Please sign in to comment.