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

cache-proxy

v1.0.0

Published

cache-proxy is a small Node package providing a simple way to put in local storage results of function calls, so next calls won't call the original function.

Downloads

3

Readme

cache-proxy

cache-proxy is a small Node package providing a simple way to put in local storage results of function calls, so next calls won't call the original function. This can be useful to minimize a number of Ajax requests for instance.

Build Status

Installation

npm install --save cache-proxy

Usage

import cacheProxy from 'cache-proxy'

// The service containing the functions we want to cache:
const service = {
  add(x, y) {
    return x + y
  },
  sub(x, y) {
    return x - y
  }
}

// Settings for the cache:
const cacheSettings = {
  add: {}
}

const cachedService = cacheProxy(service)

cachedService.add(2, 3) // calls service.add
cachedService.add(2, 3) // fetches result from local storage
cachedService.add(3, 4) // calls service.add (different arguments)
cachedService.sub(4, 3) // calls service.sub (not cached function)

Options

const cacheSettings = {
  /**
   * Each key represents the name of a method you want to cache.
   */
  [method]: {
    /**
     * By default the keys used in local storage are made with a JSON
     * stringification of the method name and the given parameters. Use
     * the `key` option to specify another way to do. You can for instance
     * use only some arguments, or use a more complex expression if
     * the parameters are not JSON-serializable.
     */
    key: (...args) => JSON.stringify({ method, args }),
    /**
     * Set this option to `true` if the function returns a promise
     * (i.e. is asynchronous). Instead of putting in cache the result
     * of the function (the promise), we'll wait for the promise to be
     * resolved to put in cache the value it is resolved with. The
     * function will still return a promise, but sometimes it will
     * be immediately resolved.
     */
    async: false
  }
}

Important notes

  • Since the result of cached functions will be put in local storage, and since local storage only allows strings to be stored in, the results must be JSON-serializable. This means they can't be object with circular references, functions, etc.
  • The keys for local storage must also be strings, so if the function arguments are not JSON-serializable, use the key setting to build a string key from arguments.

Caveats

  • When using promises (with the async setting), if the function is called several times before the first called is resolved, next calls won't use cache (since the cache exists only after the first promise is resolved).

License

Source code is distributed under GPL-v3.0 licence.