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

@avolgha/timed-cache

v1.0.1

Published

<p><h2 align="center">timed-cache</h2></p> <p align="center">Store values for a limited time in a kind of map.</p>

Downloads

4

Readme

Usage

import TimedCache from "@avolgha/timed-cache";

const sleep = (time: number) => new Promise((res) => setTimeout(res, time));

(async () => {
	const cache = new TimedCache(1000 * 3);

	cache.set("foo", "bar");

	console.log(cache.get("foo"));
	// => "bar"

	await sleep(1000 * 2);

	console.log(cache.get("foo"));
	// => "bar";

	await sleep(1000 * 2);

	console.log(cache.get("foo"));
	// => null
})();

Todos

done in v1.0.1
  • [x] Create a way to access stored keys.
  • [x] Give the developer access to the remaining time of an item.
  • [x] Create a forEach function.
  • [x] Let developer optionally choose a time for the items removal.

Configuration

You can crate a cache by instantiating the default export of the @avolgha/timed-cache package.
Optionally, you can specify the time after that an item of the map should be deleted. This time has to be specified in milliseconds.

const cache = new TimedCache();
// => items will be deleted after 5 minutes

const cache = new TimedCache(1000 * 3);
// => items will be deleted after 3 seconds

A cache can only contain one type of item. You can specify this type with a generic on the TimedCache class:

const stringCache = new TimedCache<string>();

If you don't specify any generic, TypeScript will refer to any as the default type.

Methods

To add items to the map, you can use the set method. It requires two arguments, a key and a value.

const key: string = "foo";
const value: string = "bar";

cache.set(key, value);

// you can also specify a custom time for each item:
cache.set(key, value, 1000);

If you then want to get a specific item from the cache, you can use the get method. This method only requires the key of the item you want to get.

const key = "foo";
const value = cache.get(key);

Like get you can also iterate over every item in the cache.

cache.forEach((value) => {
	// noop
});

If you want to get all the stored keys in the cache, you have to access them through the keys property.

const keys = cache.keys;

If you then not longer need the value, you can remove it with the remove function. This function aswell only needs the key of the item. And additionally, it returns the item you removed.

const key = "foo";
const removed = cache.remove(key);

To check if there is an item with a given key, you can check this with the help of the has function. It only requires the key of the item you want to check for existance.

const key = "foo";
if (cache.has(key)) {
	console.log(`"${cache.get(key)}" is in the cache!`);
}

If you want to see the remaining time of the entry in the cache, you can use the remaining function.

const key = "foo";

const timeRemaining = cache.remaining(key);