edwinm / memoize-cache-decorator Goto Github PK
View Code? Open in Web Editor NEWAdd @memoize() to your class methods to have the result cached for future method calls.
License: MIT License
Add @memoize() to your class methods to have the result cached for future method calls.
License: MIT License
timeout is reset if there are two subsequent calls to a function with different parameters (first call is a miss, the second is a hit, even tho it shouldnt be)
I resolved it like so:
const isTimeUp = Date.now() > (map.get(key)?.timeout ?? Infinity)
if (map.has(key) && (!config.ttl || !isTimeUp)) {
return map.get(key)?.result
}
else {
const result = originalFunction.apply(this, args)
let timeout = Infinity
if (config.ttl) {
timeout = Date.now() + config.ttl
}
map.set(key, {
result,
timeout
})
return result
}
btw I use stringify
from the library json-stringify-safe
in case there are circular dependencies.
whole code:
import stringify from "json-stringify-safe"
const cacheMap = new Map()
export interface IConfig {
resolver?: (...args: any[]) => string | number
ttl?: number
}
interface IMapCacheObject {
result: unknown
timeout?: number
}
export const memoize = (config: IConfig = {}) => (
// eslint-disable-next-line @typescript-eslint/ban-types
target: object,
propertyName: string,
propertyDescriptor: PropertyDescriptor
): PropertyDescriptor => {
const prop = propertyDescriptor.value
? "value"
: "get"
const originalFunction = propertyDescriptor[prop]
const map: Map<string|number|undefined, IMapCacheObject> = new Map()
propertyDescriptor[prop] = function (...args: unknown[]): unknown {
const key = config.resolver
? config.resolver.apply(this, args)
: stringify(args)
const isTimeUp = Date.now() > (map.get(key)?.timeout ?? Infinity)
if (map.has(key) && (!config.ttl || !isTimeUp)) {
return map.get(key)?.result
}
else {
const result = originalFunction.apply(this, args)
let timeout = Infinity
if (config.ttl) {
timeout = Date.now() + config.ttl
}
map.set(key, {
result,
timeout
})
return result
}
}
cacheMap.set(propertyDescriptor[prop], map)
return propertyDescriptor
}
export const clear = (fn: () => any): void => {
const map = cacheMap.get(fn)
if (map) {
map.clear()
}
}
If ttl is configurable, then we need to allow ttl param to be a sync function.
Hi Edwin,
We are using your package in our company's node backend server.
We were wondering if the cache is persisted across different workers in the node process or is it per worker?
Thank you,
Tal
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.