npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@glenstack/cf-workers-function-memoizer

v1.0.1

Published

A wrapper which utilizes Cloudflare Workers KV to memoize a function

Downloads

8

Readme

Cloudflare Workers Function Memoizer

A wrapper which utilizes Cloudflare Workers KV to memoize a function.

Installation

npm install --save @glenstack/cf-workers-function-memoizer

Usage

import { makeMemoizer } from "@glenstack/cf-workers-function-memoizer";

const memoize = makeMemoizer(CACHE_KV_NAMESPACE);

let anExpensiveFunction = async (anArgument: string) => {
  // ...
  return someResult;
};

anExpensiveFunction = memoize(
  anExpensiveFunction,
  {
    keyGenerator: (anArgument) => `anExpensiveFunction:${anArgument}`,
    resultTransformer: JSON.stringify,
    type: "json",
  },
  {
    expirationTtl: 60 * 60,
  }
);

// anExpensiveFunction is then ready to call with argument e.g. "hello"

(async () => {
  // First time, it'll execute and then cache the `JSON.stringify`-d result with `anExpensiveFunction:hello`. The cached value is set to expire in 60 minutes.
  let result = await anExpensiveFunction("hello");

  // Subsequent calls will first look up the CACHE_KV_NAMESPACE and return the result if it is found, parsing as "json".
  result = await anExpensiveFunction("hello");

  // One hour later...
  // Once the cached result is evicted, anExpensiveFunction will be re-run, and the result re-cached.
  result = await anExpensiveFunction("hello");
})();

makeMemoizer takes one parameter:

  1. The KV Namespace to be used as the cache.

It returns a function which, in turn, takes a three parameters:

  1. The expensive async function to be memoized

  2. Optionally, an object with the optional parameters:

    • keyGenerator: A function which takes the same arguments as the memoized function, and returns a string to be used as the key in the cache KV namespace. By default, it creates a string formatted like:

      functionName:arg0,arg1,arg2...

      Where functionName is the name of the memoized function and the argX's are the arguments passed to it. Note: Function names often get minified by bundlers such as webpack, so you should strongly consider defining your own keyGenerator function.

    • resultTransformer: A function which converts the memoized function's return value to a storable value in the KV namespace. Defaults to JSON.stringify.

    • type: One of the supported types to parse KV values (the inverse of resultTransformer). Defaults to 'json'.

  3. Optionally, an object of options to pass to KV when setting the value e.g. expiration times or metadata.

Finally, this can then be called in the same manner as the memoized function.