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

@cadienvan/timed-cache

v0.1.1

Published

A simple in-memory time-based cache for both objects and function execution.

Downloads

5

Readme

What is this?

A simple in-memory time-based cache for both objects and function execution.

How do I install it?

You can install it by using the following command:

npm install @cadienvan/timed-cache

Why did you build it?

I needed a simple cache for a function that was being called repeatedly with the same arguments. I wanted to cache the results for a certain amount of time, and then re-run the function when the cache expired.

How can I use it?

You can import and instance a new TimedCache object as follows:

import { TimedCache } from '@cadienvan/timed-cache';
const cache = new TimedCache();

Look at the demo folder in the GitHub Repository in order to have some proofs of concept considering both synchronous and asynchronous functions.

Does it support async functions?

Yes, it does. You can use it with both synchronous and asynchronous functions.
Look at the demo folder in the GitHub Repository for an example.

Which parameters are available?

You can pass a parameter to the TimedCache, which defines the amount of items the cache can contain. If you don't pass any parameter, the cache will contain 100 elements.
As soon as a new element is added to the cache, the oldest one will be removed.

How can I add an element to the cache?

You can add an element to the cache by using the set method.

cache.set('key', 'value');

How can I retrieve an element from the cache?

You can retrieve an element from the cache by using the get method.

cache.get('key');

How can I remove an element from the cache?

You can remove an element from the cache by using the delete method.

cache.delete('key');

How can I clear the cache?

You can clear the cache by using the clear method.

cache.clear();

How can I get the size of the cache?

You can get the size of the cache by using the size property.

cache.size;

How can I get the keys of the cache?

You can get the keys of the cache by using the keys method.

cache.keys();

How can I get the values of the cache?

You can get the values of the cache by using the values method.

cache.values();

How can I get the entries of the cache?

You can get the entries ([key, value] pairs) of the cache by using the entries method.

cache.entries();

How can I iterate over the cache?

You can iterate over the cache by using the forEach method.

cache.forEach((value, key) => {
  console.log(key, value);
});

How can I check if an element is in the cache?

You can check if an element is in the cache by using the has method.

cache.has('key');

How can I get the time to live of an element?

You can get the time to live of an element by using the ttl method.

cache.ttl('key');

Does the class support key-based expiration?

No, it doesn't. You can use the @cadienvan/key-value-cache library in order to achieve this.

How does it work under the hood?

The cache is a simple object that stores the results of a function call in memory leveraging the Map constructor.
The cache is time-based, so the results are only valid for a certain amount of time. If the cache expires, the wrapped object / functions is re-run and the results are cached again.

ToDo

  • [ ] Add tests