Last active
April 30, 2023 21:54
-
-
Save christopherbauer/aeaa866adb2341ffcd3456dc11abd348 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Type Branding | |
*/ | |
type Brand<K, T> = K & { __type: T }; | |
/** | |
* CacheKey branded type | |
*/ | |
export type CacheKey = Brand<string, "CacheKey">; | |
/** | |
* Memory store | |
*/ | |
export let cache: Record<string, any> = {}; | |
/** | |
* Helper method that returns all Branded cache keys | |
* @returns The keys in the cache as CacheKeys for ejection purposes | |
*/ | |
export const CacheKeys = () => Object.keys(cache) as CacheKey[]; | |
/** | |
* Flushes the cache - use at your own risk! | |
*/ | |
export const CacheFlush = () => { | |
const count = CacheKeys().length; | |
cache = {}; | |
console.log(`Cleared ${count} keys`); | |
}; | |
/** | |
* Removes a value from the cache | |
* @param key The cache key to remove from the cache | |
* @returns Boolean value indicating if the deletion was successful | |
*/ | |
export const CacheEject = (key: CacheKey) => delete cache[key]; | |
/** | |
* Decorator that works with the memory cache | |
* @param group The group or prefix for the keys | |
* @returns | |
*/ | |
export const Cached = (group: string) => { | |
return function ( | |
originalMethod: Function, | |
_context: ClassMethodDecoratorContext | |
) { | |
function ReplacementMethod(this: any, ...args: any[]) { | |
const gennedKey = [group, ...args].map(String).join(","); | |
if (!cache[gennedKey]) { | |
cache[gennedKey] = originalMethod.call(this, ...args); | |
} | |
return cache[gennedKey]; | |
} | |
return ReplacementMethod; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment