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-point

v3.0.0

Published

Simple, filesystem-backed memoisation cache.

Downloads

853,210

Readme

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

cache-point

Simple, filesystem-backed memoisation cache. Use to cache the output of expensive operations speeding up future invocations with the same input.

Synopsis

import Cache from 'cache-point'
import { setTimeout as sleep } from 'node:timers/promises'

/* mock function to simulate a slow, remote request */
async function fetchUser (id) {
  await sleep(1000)
  return { id, name: 'Layla' }
}

class Users {
  constructor () {
    this.cache = new Cache({ dir: 'tmp/example' })
  }

  async getUser (id) {
    let user
    try {
      /* cache.read() will resolve on hit, reject on miss */
      user = await this.cache.read(id)
    } catch (err) {
      if (err.code === 'ENOENT') {
        /* cache miss, fetch remote user */
        user = await fetchUser(id)
        this.cache.write(id, user)
      }
    }
    return user
  }
}

console.time('getUser')
const users = new Users()
const user = await users.getUser(10)
console.timeEnd('getUser')
console.log(user)

The first invocation will take 1 second while the remote user is fetched.

$ node example/simple.js
getUser: 1.025s
{ id: 10, name: 'Layla' }

Since the cache is now warm, future invocations will be fast.

$ node example/simple.js
getUser: 17.07ms
{ id: 10, name: 'Layla' }

API Reference

cache-point

Cache ⏏

Kind: Exported class

new Cache([options])

| Param | Type | | --- | --- | | [options] | object | | [options.dir] | string |

cache.dir : string

Current cache directory. Can be changed at any time.

Kind: instance property of Cache

cache.read(keys) ⇒ Promise

A cache hit resolves with the stored value, a miss rejects with an ENOENT error code.

Kind: instance method of Cache
Throws:

  • ENOENT

| Param | Type | Description | | --- | --- | --- | | keys | * | One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |

cache.readSync(keys) ⇒ string

A cache hit returns the stored value, a miss returns null.

Kind: instance method of Cache

| Param | Type | Description | | --- | --- | --- | | keys | * | One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |

cache.write(keys, content) ⇒ Promise

Write some data to the cache. Returns a promise which resolves when the write is complete.

Kind: instance method of Cache

| Param | Type | Description | | --- | --- | --- | | keys | * | One or more values to index the data, e.g. a request object or set of function args. | | content | * | the data to store |

cache.writeSync(keys, content)

Write some data to the cache with a key.

Kind: instance method of Cache

| Param | Type | Description | | --- | --- | --- | | keys | * | One or more values to index the data, e.g. a request object or set of function args. | | content | * | the data to store |

cache.getChecksum(keys) ⇒ string

Used internally to convert a key value into a hex checksum. Override if for some reason you need a different hashing strategy.

Kind: instance method of Cache

| Param | Type | Description | | --- | --- | --- | | keys | * | One or more values to index the data, e.g. a request object or set of function args. |

cache.clear() ⇒ Promise

Clears the cache. Returns a promise which resolves once the cache is clear.

Kind: instance method of Cache

cache.remove() ⇒ Promise

Clears and removes the cache directory. Returns a promise which resolves once the remove is complete.

Kind: instance method of Cache


© 2016-24 Lloyd Brookes <[email protected]>.

Tested by test-runner. Documented by jsdoc-to-markdown.