Socket
Socket
Sign inDemoInstall

quick-lru

Package Overview
Dependencies
0
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    quick-lru

Simple “Least Recently Used” (LRU) cache


Version published
Weekly downloads
18M
decreased by-18.89%
Maintainers
1
Install size
15.9 kB
Created
Weekly downloads
 

Package description

What is quick-lru?

The quick-lru package is a fast and efficient least-recently-used (LRU) cache implementation in JavaScript. It is designed to store a limited amount of items and evict the least-recently-used items once the limit is reached. This package is useful for caching results, reducing the number of expensive operations (like I/O), and improving application performance.

What are quick-lru's main functionalities?

Set and Get Items

This feature allows you to set and retrieve items from the cache using the 'set' and 'get' methods. The 'maxSize' option specifies the maximum number of items the cache can hold.

{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
const value = lru.get('key'); // 'value'"}

Cache Eviction

This feature automatically evicts the least-recently-used items when the cache reaches its maximum size. In this example, after adding three items to a cache with a maxSize of 2, the first item is evicted.

{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 2});
lru.set('a', 1);
lru.set('b', 2);
lru.set('c', 3);
// At this point, 'a' is evicted because it is the least recently used item"}

Cache Deletion and Clearing

This feature allows you to delete individual items or clear the entire cache. The 'delete' method removes a specific item, and the 'clear' method removes all items.

{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
lru.delete('key'); // Removes 'key' from the cache
lru.clear(); // Removes all items from the cache"}

Cache Size and Existence Check

This feature provides methods to check the current size of the cache and whether a specific key exists in the cache. The 'size' property gives the number of items in the cache, and the 'has' method checks for the existence of a key.

{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
const size = lru.size; // 1
const hasKey = lru.has('key'); // true"}

Other packages similar to quick-lru

Readme

Source

quick-lru Coverage Status

Simple “Least Recently Used” (LRU) cache

Useful when you need to cache something and limit memory usage.

Inspired by the hashlru algorithm, but instead uses Map to support keys of any type, not just strings, and values can be undefined.

Install

$ npm install quick-lru

Usage

import QuickLRU from 'quick-lru';

const lru = new QuickLRU({maxSize: 1000});

lru.set('🦄', '🌈');

lru.has('🦄');
//=> true

lru.get('🦄');
//=> '🌈'

API

new QuickLRU(options?)

Returns a new instance.

It's a Map subclass.

options

Type: object

maxSize

Required
Type: number

The maximum number of items before evicting the least recently used items.

maxAge

Type: number
Default: Infinity

The maximum number of milliseconds an item should remain in cache. By default maxAge will be Infinity, which means that items will never expire.

Lazy expiration happens upon the next write or read call.

Individual expiration of an item can be specified by the set(key, value, options) method.

onEviction

Optional
Type: (key, value) => void

Called right before an item is evicted from the cache.

Useful for side effects or for items like object URLs that need explicit cleanup (revokeObjectURL).

Instance

The instance is an Iterable of [key, value] pairs so you can use it directly in a for…of loop.

Both key and value can be of any type.

.set(key, value, options?)

Set an item. Returns the instance.

Individual expiration of an item can be specified with the maxAge option. If not specified, the global maxAge value will be used in case it is specified on the constructor, otherwise the item will never expire.

.get(key)

Get an item.

.has(key)

Check if an item exists.

.peek(key)

Get an item without marking it as recently used.

.delete(key)

Delete an item.

Returns true if the item is removed or false if the item doesn't exist.

.clear()

Delete all items.

.resize(maxSize)

Update the maxSize, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.

Useful for on-the-fly tuning of cache sizes in live systems.

.keys()

Iterable for all the keys.

.values()

Iterable for all the values.

.entriesAscending()

Iterable for all entries, starting with the oldest (ascending in recency).

.entriesDescending()

Iterable for all entries, starting with the newest (descending in recency).

.entries()

Iterable for all entries, starting with the newest (ascending in recency).

This method exists for Map compatibility. Prefer .entriesAscending() instead.

.forEach(callbackFunction, thisArgument)

Loop over entries calling the callbackFunction for each entry (ascending in recency).

This method exists for Map compatibility. Prefer .entriesAscending() instead.

.size

The stored item count.

  • yocto-queue - Tiny queue data structure

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Keywords

FAQs

Last updated on 26 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc