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

Overview

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 "../dist/index.js";
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");

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.

You might also like...

Memory lane is a website where you can create visual timelines of moments in time.

Memory lane is a website where you can create visual timelines of moments in time.

Memory Lane Moments in Time, All in One Place Description Memory Lane is a web app where users can create visually appealing, minimalistic timelines e

Jun 13, 2022

ClientDB is an open source in-memory database for enabling real-time web apps.

ClientDB ClientDB is an open source in-memory database for enabling real-time web apps. Build fast, scalable apps that feel silky smooth for users. In

Dec 24, 2022

ClientDB is an open source in-memory database for enabling real-time web apps.

ClientDB ClientDB is an open-source in-memory database for enabling real-time web apps. Build fast, scalable apps that feel silky smooth for users. In

Aug 27, 2022

ROP userland execution for PS5 (4.03)

ROP userland execution for PS5 (4.03)

# Exploring the Playstation 5 Security - Userland Introduction The PlayStation 5 was released on November 12th 2020. While it's similar to the PS4 in

Dec 2, 2022

"Lerna & Distributed Task Execution" Example

Lerna Distributed Task Execution (DTE) Example/Benchmark On how to make your CI 23 times faster with a small config change New versions of Lerna can u

Nov 27, 2022

jQuery Plugin For Delayed Event Execution

bindWithDelay jQuery plugin Author: Brian Grinstead MIT license: http://www.opensource.org/licenses/mit-license.php http://github.com/bgrins/bindWith

Dec 31, 2022

Next-level mongoose caching layer with event based cache clearing

SpeedGoose ## About The Project This project is a next-level mongoose caching library which is fully written in typescript. It's caching on two levels

Dec 15, 2022

Binary-encoded serialization of JavaScript objects with generator-based parser and serializer

YaBSON Schemaless binary-encoded serialization of JavaScript objects with generator-based parser and serializer This library is designed to transfer l

Aug 9, 2022

Logs the output, time, arguments, and stacktrace of any function when it's called in a gorgeous way.

Function.prototype.log Logs the output, time, arguments, and stacktrace of any function when it's called. How to use: Like this: function yourFunction

Apr 9, 2022
Owner
cadienvan
Full-Stack Developer
cadienvan
A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

remote-map-cache A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fa

Tomato6966 8 Oct 31, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
Brain wallet using both language and visual memory.

*Check out the big brain on Brett! You're a smart motherfvcker, that's right.* -- Pulp fiction ?? SUPER BRAIN WALLET ?? Use your brain power to the ma

Code's All Right ™ 4 Jun 9, 2022
startupDB is an Express middleware function implementing a high-performance in-memory database

startupDB startupDB is a database designed to create REST APIs. It is implemented as an Express middleware function and allows for easy implementation

Jeroen de Vries 8 Jul 26, 2022
Open, extensible, small and simple behaviour-graph execution engine

Behave-Graph Behave-Graph is a standalone library that implements the concept of "behavior graphs" as a portable TypeScript library with no external r

Ben Houston 167 Dec 29, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022
Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Antonio Stoilkov 14 Dec 8, 2022