A cache that automatically removes the least-recently-used items

Overview

Dadado · build npm license

A cache that automatically removes the least-recently-used items.

Features

  • Fast LRU implementation
  • Gzipped version has less than 400 bytes
  • Minimalist API
  • Supports to both Node and Browser
  • Supports to Types
  • Zero-dependency

Installation

To install dadado, execute:

$ npm install dadado

or

$ yarn add dadado

Quickstart

Dadado is a cache class which is designed to keep storing just the most recently used items. To ensure it, the class receives a capacity, that will be used to discard the least recently used items when the capacity is reached.

import Dadado from 'dadado';

// `capacity` is required and it must to be a positive integer
const cache = new Dadado(3);

cache.setItem('key1', 'value1');
cache.setItem('key2', 'value2');
cache.getItem('key1'); // 'value1'
cache.setItem('key3', 'value3');

// At this point, the item associeted with `key2` will be evicted.
// It happens because the capacity was reached and `key1` was used before `key2`.
cache.setItem('key4', 'value4');

cache.size(); // 3
cache.contains('key2'); // false
cache.contains('key1'); // true

Methods

contains(key: any): boolean
Checks if the given key exist within the cache

setItem(key: any, value: any): boolean
Adds the key-value pair to the cache if the key is not in the cache yet. Otherwise, if the key exists, updates the value of the key. In case the current number of keys exceeds the capacity, then it evicts the least recently used key that is not marked as persistent.

getItem(key: any): any
Retrieves the value associeted with the given key if it exists in the cache. If the key is not in the cache, it returns undefined.

removeItem(key: any): boolean
Deletes item and returns true if the item existed in the cache - persistent will be removed as well. Returns false if the element doesn't exist in the cache.

setPersistent(key: any): void
Makes item persistent, i.e the item can no longer be automatically evicted. However, the item still will be removed when invoking removeItem or clear.

import Dadado from 'dadado';

const cache = new Dadado(3);
cache.setItem('key1', 'value1');
cache.setPersistent('key1');
cache.setItem('key2', 'value2');
cache.setItem('key3', 'value3');

// At this point the item associeted with `key2` will be evicted
// since the `key1` is marked as `persistent` and can not be evicted.
cache.setItem('key4', 'value4');

cache.contains('key1'); // true
cache.contains('key2'); // false

removePersistent(key: any): void
Makes item no longer a persistent item.

togglePersistent(key: any): void
Makes item persistent if it was not yet, or otherwise undo the persistent flag.

toArray(): T[][]
Returns an Array based in the current cache with each key-value pair sorted by least-recently-used.

size(): number
Returns the cache size.

clear(): void
Remove all items - even the persistent ones - from the cache.

LICENSE

MIT

You might also like...

Tiny JavaScript library (1kB) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value.

Zero dependency tiny JavaScript library (1kB bytes) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value.

Nov 8, 2022

A less plugin that removes ant-design global styles

less-plugin-remove-antd-global-styles This is a less plugin that removes ant-design global styles. It works well with vite, webpack, rollup and babel-

Nov 22, 2022

A plugin that will automatically download PDFs of zotero items from sci-hub

A plugin that will automatically download PDFs of zotero items from sci-hub

Zotero Scihub This is an add-on for Zotero and Juris-M that enables automatic download of PDFs for items with a DOI. Quick Start Guide Install Downloa

Jan 8, 2023

Automatically generate unit price for applicable items when shopping online.

Automatically generate unit price for applicable items when shopping online.

Unit Price Helper Unit Price Helper is an Google Chrome extension that allows to display the prices per unit for the products on multiple shopping sit

Nov 18, 2022

This restaurant project is a SPA (single-page application) website. The user can navigate between the home, menu and contact page. I used the MealDB API to display some menu items.

This restaurant project is a SPA (single-page application) website. The user can navigate between the home, menu and contact page. I used the MealDB API to display some menu items.

Fresh Cuisine This restaurant project is from the Odin Project and it is a SPA (single-page application) website. The user can navigate between the ho

Nov 2, 2022

A javascript written program that can be used to automatically join BloxFlip Rains.

Toxic Rainer Auto-BloxFlip Rain Joiner Toxic Rainer is a web-socket based background running program used to automatically join BloxFlip Rains to get

Jul 10, 2022

Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.

twitter-text This repository is a collection of libraries and conformance tests to standardize parsing of Tweet text. It synchronizes development, tes

Jan 8, 2023

The Taste food web app is our JavaScript capstone project, Taste food is a web application based on an external food API, showing data about Italian foods and we used Used involvement API to record the different user interactions (likes, comments).

taste-food The Taste food web app is our JavaScript capstone project, Taste food is a web application based on an external food API TheMealDB, showing

Aug 10, 2022
Releases(v0.2.1)
  • v0.2.1(Mar 16, 2022)

    Dadado is a cache that automatically removes the least-used items.

    What's new?

    • Improved the performance of setItem
    • Implemented the toArray method
    • Updated the project name :)
    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Feb 12, 2022)

    TheStorage is a storage that automatically removes the least-recently-used items.

    What's new?

    • Improved algorithm to store new items
    • Rename methods to be consistent with localStorage and sessionStorage
    Source code(tar.gz)
    Source code(zip)
Owner
Evandro Leopoldino Gonçalves
senior learner 🤓
Evandro Leopoldino Gonçalves
A simplified list crud that adds, removes and edits items

To-do list This project is as simplified todo list crud. Built With HTML CSS JavaScript Webpack Live version See live version Getting Started To get a

Fernando Saldaña 7 Apr 2, 2022
Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or enter your email address and more.

Demodal Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or en

Elbert Alias 225 Jan 4, 2023
A website that will curate recently-asked interview questions from FAANG+ to help people practice & prep!

defaang.io A website that will curate recently-asked interview questions from FAANG+ to help people practice & prep! The questions will be submitted a

YK 488 Dec 12, 2022
Dynamic island style widget for displaying what you're recently played on Spotify.

Spotify Island Dynamic island style widget for displaying what you're recently played on Spotify. ?? Check the demo spotify-island.mov 1. Get Spotify

Nurçin 5 Nov 24, 2022
A Zotero add-on that scans your Markdown reading notes, tags the associated Zotero items, and lets you open notes for the Zotero items in Obsidian.

Zotero Obsidian Citations Adds colored tags to Zotero items that have associated Markdown notes stored in an external folder. Open an associated Markd

Dae 210 Jan 4, 2023
The app helps you to add todo items to your list, mark completed ones and also delete finished items. Its a handy tool for your day today activies. Check out the live demo.

Todo List App The app helps you to add todo items to your list, mark completed ones and also delete finished items. Its a handy tool for your day toda

Atugonza ( Billions ) Joel 14 Apr 22, 2022
A secondhand marketplace where you can post items for sale, interact with sellers, save items you are interested in.

Curbside - the secondhand market place that's actually pleasant to use Post items for sale, interact with sellers, save items you are interested in. A

Curbside 14 Sep 9, 2022
A single-page application that allow users to add their To Do items. The items could be checked as completed and the completed task can be removed. Built with JavaScript, HTML and CSS

To Do list Application This is a single page application that allows users to keep track of their tasks. Users can add the task and also check the che

Micheal Oguntayo 4 Oct 14, 2022
A simple single page web-app that stores, removes and edits books' names in the browser's local storage.

A simple single page web-app that stores, removes and edits books' names in the browser's local storage. Made with Webpack bundler

Alejandro Neira 5 Apr 1, 2022