-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sliding expiration cache (#18)
- Loading branch information
Showing
10 changed files
with
514 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
export class MapUtils<K, V> { | ||
protected map: Map<K, V> = new Map<K, V>(); | ||
|
||
get size(): number { | ||
return this.map.size; | ||
} | ||
|
||
get keys() { | ||
return this.map.keys(); | ||
} | ||
|
||
get entries() { | ||
return this.map.entries(); | ||
} | ||
|
||
get(key: K): V | undefined { | ||
return this.map.get(key); | ||
} | ||
|
||
clear() { | ||
this.map.clear(); | ||
} | ||
|
||
computeIfPresent(key: K, remappingFunc: (key: K, existingValue: V) => V | null): V | undefined { | ||
const existingValue: V | undefined = this.map.get(key); | ||
if (existingValue === undefined) { | ||
return undefined; | ||
} | ||
const newValue: any = remappingFunc(key, existingValue); | ||
if (newValue !== null) { | ||
this.map.set(key, newValue); | ||
return newValue; | ||
} else { | ||
this.map.delete(key); | ||
return undefined; | ||
} | ||
} | ||
|
||
computeIfAbsent(key: K, mappingFunc: (key: K) => V | null): V | undefined { | ||
const value: V | undefined = this.map.get(key); | ||
if (value == undefined) { | ||
const newValue: V | null = mappingFunc(key); | ||
if (newValue !== null) { | ||
this.map.set(key, newValue); | ||
return newValue; | ||
} | ||
return undefined; | ||
} | ||
return value; | ||
} | ||
|
||
putIfAbsent(key: K, newValue: V): V | undefined { | ||
const existingValue: V | undefined = this.map.get(key); | ||
if (existingValue === undefined) { | ||
this.map.set(key, newValue); | ||
return newValue; | ||
} | ||
return existingValue; | ||
} | ||
|
||
remove(key: K): V | undefined { | ||
const value = this.map.get(key); | ||
this.map.delete(key); | ||
return value; | ||
} | ||
|
||
removeIf(predicate: (v: any, k: any) => V): boolean { | ||
const originalSize = this.size; | ||
this.map.forEach((v, k) => { | ||
if (predicate(v, k)) { | ||
this.remove(k); | ||
} | ||
}); | ||
return this.size < originalSize; | ||
} | ||
|
||
removeMatchingValues(removalValues: any[]): boolean { | ||
const originalSize = this.size; | ||
this.map.forEach((v, k) => { | ||
if (removalValues.includes(v)) { | ||
this.remove(k); | ||
} | ||
}); | ||
return this.size < originalSize; | ||
} | ||
|
||
applyIf(predicate: (v: any, k: any) => V, apply: (v: any, k: any) => V): void { | ||
const originalSize = this.size; | ||
this.map.forEach((v, k) => { | ||
if (predicate(v, k)) { | ||
apply(v, k); | ||
} | ||
}); | ||
} | ||
} |
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,122 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { MapUtils } from "./map_utils"; | ||
import { getTimeInNanos } from "aws-wrapper-common-lib/lib/utils/utils"; | ||
|
||
class CacheItem<V> { | ||
private _item: V; | ||
private _expirationTimeNanos: bigint; | ||
|
||
constructor(item: V, expirationTimeNanos: bigint) { | ||
this._item = item; | ||
this._expirationTimeNanos = expirationTimeNanos; | ||
} | ||
|
||
get item(): V { | ||
return this._item; | ||
} | ||
|
||
get expirationTimeNs(): bigint { | ||
return this._expirationTimeNanos; | ||
} | ||
|
||
updateExpiration(expirationIntervalNanos: bigint): CacheItem<V> { | ||
this._expirationTimeNanos = getTimeInNanos() + expirationIntervalNanos; | ||
return this; | ||
} | ||
} | ||
|
||
export class SlidingExpirationCache<K, V> { | ||
private _cleanupIntervalNanos: bigint = BigInt(10 * 60_000_000_000); // 10 minutes | ||
private readonly _shouldDisposeFunc?: (item: V) => boolean; | ||
private readonly _itemDisposalFunc?: (item: V) => void; | ||
private _map: MapUtils<K, CacheItem<V>> = new MapUtils<K, CacheItem<V>>(); | ||
private _cleanupTimeNanos: bigint; | ||
|
||
constructor(cleanupIntervalNanos: bigint, shouldDisposeFunc?: (item: V) => boolean, itemDisposalFunc?: (item: V) => void) { | ||
this._cleanupIntervalNanos = cleanupIntervalNanos; | ||
this._shouldDisposeFunc = shouldDisposeFunc; | ||
this._itemDisposalFunc = itemDisposalFunc; | ||
this._cleanupTimeNanos = getTimeInNanos() + this._cleanupIntervalNanos; | ||
} | ||
|
||
get size(): number { | ||
return this._map.size; | ||
} | ||
|
||
set cleanupIntervalNs(value: bigint) { | ||
this._cleanupIntervalNanos = value; | ||
} | ||
|
||
computeIfAbsent(key: K, mappingFunc: (key: K) => V, itemExpirationNanos: bigint): V | null { | ||
this.cleanUp(); | ||
const cacheItem = this._map.computeIfAbsent(key, (k) => new CacheItem(mappingFunc(k), getTimeInNanos() + itemExpirationNanos)); | ||
return cacheItem?.updateExpiration(itemExpirationNanos).item ?? null; | ||
} | ||
|
||
get(key: K): V | undefined { | ||
this.cleanUp(); | ||
const cacheItem = this._map.get(key); | ||
return cacheItem?.item ?? undefined; | ||
} | ||
|
||
remove(key: K): void { | ||
this.removeAndDispose(key); | ||
this.cleanUp(); | ||
} | ||
|
||
removeAndDispose(key: K): void { | ||
const cacheItem = this._map.remove(key); | ||
if (cacheItem != null && this._itemDisposalFunc != null) { | ||
this._itemDisposalFunc(cacheItem.item); | ||
} | ||
} | ||
|
||
removeIfExpired(key: K): void { | ||
const cacheItem = this._map.get(key); | ||
if (cacheItem == null || this.shouldCleanupItem(cacheItem)) { | ||
this.removeAndDispose(key); | ||
} | ||
} | ||
|
||
shouldCleanupItem(cacheItem: CacheItem<V>): boolean { | ||
if (this._shouldDisposeFunc != null) { | ||
return getTimeInNanos() > cacheItem.expirationTimeNs && this._shouldDisposeFunc(cacheItem.item); | ||
} | ||
return getTimeInNanos() > cacheItem.expirationTimeNs; | ||
} | ||
|
||
clear(): void { | ||
for (const [key, val] of this._map.entries) { | ||
if (val !== undefined && this._itemDisposalFunc !== undefined) { | ||
this._itemDisposalFunc(val.item); | ||
} | ||
} | ||
this._map.clear(); | ||
} | ||
|
||
protected cleanUp() { | ||
const currentTime = getTimeInNanos(); | ||
if (this._cleanupTimeNanos > currentTime) { | ||
return; | ||
} | ||
this._cleanupTimeNanos = currentTime + this._cleanupIntervalNanos; | ||
for (const k of this._map.keys) { | ||
this.removeIfExpired(k); | ||
} | ||
} | ||
} |
File renamed without changes.
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
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
Oops, something went wrong.