0.0.4 • Published 2 years ago

@mdxvac/mem-cache v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@mdxvac/mem-cache

DEPRECATED: Please consider using the plugin astro-m2dx, which bundles all features from the @mdxvac plugins in one plugin (completely opt-in).

Simple typed in-memory cache for stateless functions.

Content

What is this?

This package is a simple TypeScript class, that allows to wrap a stateless function in a cache.

When should I use this?

If you have a computation-intensive or I/O-heavy (quasi) stateless function, that will return the same value for each invocation with the same parameters, then you can wrap it into this cache and use cached values for repeated calls.

I use it e.g. in a Static Site Generator to access values from configuration files.

Install

npm install -D @mdxvac/mem-cache

Use

function expensiveComputation(a: string, b: number) {
  return `${a} - ${b}`;
}
const cachedComputation = new MemCache(expensiveComputation);

const result = cachedComputation.get('a', 8);

Of course, you can also use arrow functions to initialize the cache:

const cachedComputation = new MemCache((a: string, b: number): string => {
  return `${a} - ${b}`;
});