Socket
Socket
Sign inDemoInstall

@alloc/quick-lru

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @alloc/quick-lru

Simple “Least Recently Used” (LRU) cache


Version published
Weekly downloads
6.2M
decreased by-1.13%
Maintainers
1
Install size
14.9 kB
Created
Weekly downloads
 

Package description

What is @alloc/quick-lru?

The @alloc/quick-lru package is a fast, lightweight least recently used (LRU) cache implementation. It is designed to efficiently store and retrieve items based on their usage, ensuring that the most recently accessed items are kept in memory while older items are discarded when the cache reaches its capacity. This package is useful for optimizing performance in applications where accessing data from memory is preferable to repeatedly fetching it from slower sources like databases or file systems.

What are @alloc/quick-lru's main functionalities?

Creating and using an LRU cache

This feature demonstrates how to create an LRU cache with a maximum size, add an item to the cache, retrieve an item, check if an item exists in the cache, and delete an item from the cache.

{"const QuickLRU = require('@alloc/quick-lru');\nconst lru = new QuickLRU({ maxSize: 100 });\nlru.set('key', 'value');\nconsole.log(lru.get('key')); // 'value'\nconsole.log(lru.has('key')); // true\nlru.delete('key');\nconsole.log(lru.has('key')); // false"}

Iterating over cache items

This feature shows how to iterate over the items in the cache. It's useful for scenarios where you need to perform operations on all items stored in the cache.

{"const QuickLRU = require('@alloc/quick-lru');\nconst lru = new QuickLRU({ maxSize: 2 });\nlru.set('a', 1);\nlru.set('b', 2);\nfor (const [key, value] of lru) {\n  console.log(key, value);\n}"}

Other packages similar to @alloc/quick-lru

Readme

Source

quick-lru Build Status 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

const QuickLRU = require('quick-lru');

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

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

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

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

API

new QuickLRU(options?)

Returns a new instance.

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 iterable 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).

.size

The stored item count.


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 06 Apr 2021

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