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

rache

v1.0.0

Published

Random-eviction cache

Downloads

6,335

Readme

Rache

A random-eviction cache which imposes a global cache-size limit on all the caches derived from it.

Useful when you have many caches and want to limit their memory usage as a whole, instead of per cache.

The get and set operations as well as delete (and cache eviction) take constant time.

Install

npm i rache

Usage

const Rache = require('rache')

const cache = new Rache({ maxSize: 3 })
const cache2 = cache.sub()
const cache3 = cache.sub()

cache.set('key', 'value')
cache2.set('key', 'otherValue')
cache3.set('some', 'thing')

// cache 1 is a separate cache from cache2 and cache3
console.log('cached:', cache.get('key')) // 'value'

// But they share the same global size
console.log(cache.globalSize, 'of', cache.maxSize) // 3 of 3

cache.set('key2', 'another value')
// The cache was full, so one of the existing 3 entries got evicted

console.log(cache.globalSize, 'of', cache.maxSize) // 3 of 3

API

const cache = new Rache({ maxSize=65536 })

Create a new cache.

maxSize is the maximum amount of entries globally, across this cache and all derived caches (derived with cache.sub() or Rache.from(cache)).

const subCache = cache.sub()

Create a new cache which shares the global memory limit with the original cache.

const aCache = Rache.from(cache?)

Create a new rache instance.

If an existing cache is passed in, it will create a sub-cache (equivalent to aCache = cache.sub()).

Otherwise (if no cache or a falsy value is passed in), it will create a new cache (equivalent to aCache = new Rache()).

cache.globalSize

The current amount of entries across all caches.

cache.maxSize

The maximum amount of entries across all caches.

cache.size

The amount of entries in this specific cache.

cache.set(key, value)

Set the key to the given value.

If the global cache size was at the limit, a random old entry is evicted from the cache.

cache.delete(key)

Delete the entry corresponding to key, if any.

Returns true if an entry was deleted, false otherwise.

cache.get(key)

Returns the value corresponding to the given key, or undefined if there is none.

cache.keys()

Returns an iterator over the keys of this particular cache.

cache.values()

Returns an iterator over the values of this particular cache.

cache.clear()

Clear all entries from this particular cache.

After clearing, new entries can again be added to the cache.

cache.destroy()

Destroys the cache. The cache should no longer be used after it has been destroyed.