GithubHelp home page GithubHelp logo

edwinm / memoize-cache-decorator Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 0.0 542 KB

Add @memoize() to your class methods to have the result cached for future method calls.

License: MIT License

TypeScript 96.89% JavaScript 2.34% Shell 0.77%

memoize-cache-decorator's Introduction

Edwin Martin - Freelance frontend engineer

memoize-cache-decorator's People

Contributors

dependabot[bot] avatar edwinm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

memoize-cache-decorator's Issues

if two subsequent calls with different parameters are called its a cache hit

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()
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.