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

@alexsasharegan/simple-cache

v3.3.3

Published

A basic key value store with bounded capacity for simple caching.

Downloads

1,332

Readme

simple-cache

npm npm downloads GitHub issues Travis Coverage Status GitHub license

A basic key value store with bounded capacity for simple caching.

Install

npm install @alexsasharegan/simple-cache

Documentation

Visit the API Documentation here.

Cache is an interface for caching key/value pairs of types K/V. It's primary functionality includes:

  • Cache.write: write a value at a key
  • Cache.read: read a value from a key
  • Cache.get: read a value as an Option<V> (maybe type)
  • Cache.remove: remove a value by key
  • Cache.clear: empty the cache

Usage

Creating a cache looks like this in plain JavaScript:

// Creates a cache with a capacity of 10 items. The second parameter is the
// "type label". It serves as a convenience option for more meaningful
// `toString` behavior.
let userCache = SimpleCache(10, { key: "ID", value: "User" })
userCache.toString()
// SimpleCache<ID, User> { size: 0, capacity: 10 }

If you're using TypeScript, you need to create a cache by declaring the key and value types as generic parameters:

interface User {
	id: number
	name: string
}

let userCache = SimpleCache<number, User>(10, { key: "ID", value: "User" })
userCache.toString()
// SimpleCache<ID, User> { size: 0, capacity: 10 }

Here are some examples of the five main interaction methods shown in REPL style. If you're curious to learn more about the Option type returned from Cache.get, visit the safe-types repository.

let userA = { name: "User A", id: 1 }
let userB = { name: "User B", id: 2 }

userCache.write(userA.id, userA)
userCache.write(userB.id, userB)
userCache.size()
// 2

userCache.read(1)
// { name: "User A", id: 1 }
userCache.read(2)
// { name: "User B", id: 2 }
userCache.get(2)
// Some<{ name: "User B", id: 2 }>
userCache.read(3)
// undefined
userCache.get(3)
// None

userCache.remove(1)
userCache.size()
// 1
userCache.read(1)
// undefined
userCache.clear()
userCache.size()
// 0

There is an Ephemeral Cache available. It caches items for a duration that's given in milliseconds.

// Sets a capacity of 10 users and a max lifetime of 5 seconds
let userCache = EphemeralCache<number, User>(10, 5000, {
	key: "ID",
	value: "User",
})

userCache.write(1, { name: "Temp", id: 1 })

setTimeout(() => {
	userCache.read(1)
	// undefined
}, 5001)

userCache.read(1)
// { name: "Temp", id: 1 }