Socket
Socket
Sign inDemoInstall

ylru

Package Overview
Dependencies
0
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ylru

Extends LRU base on hashlru


Version published
Weekly downloads
1.5M
decreased by-1.09%
Maintainers
3
Install size
8.80 kB
Created
Weekly downloads
 

Readme

Source

ylru

NPM version Node.js CI Test coverage Known Vulnerabilities npm download

hashlru inspired

hashlru is the Simpler, faster LRU cache algorithm. Please checkout algorithm and complexity on hashlru.

ylru extends some features base on hashlru:

  • cache value can be expired.
  • cache value can be empty value, e.g.: null, undefined, '', 0

Usage

const LRU = require('ylru');

const lru = new LRU(100);
lru.set(key, value);
lru.get(key);

// value2 will be expired after 5000ms
lru.set(key2, value2, { maxAge: 5000 });
// get key and update expired
lru.get(key2, { maxAge: 5000 });

API

LRU(max) => lru

initialize a lru object.

lru.get(key[, options]) => value | null

  • {Number} options.maxAge: update expire time when get, value will become undefined after maxAge pass.

Returns the value in the cache.

lru.set(key, value[, options])

  • {Number} options.maxAge: value will become undefined after maxAge pass. If maxAge not set, value will be never expired.

Set the value for key.

lru.keys()

Get all unexpired cache keys from lru, due to the strategy of ylru, the keys' length may greater than max.

const lru = new LRU(3);
lru.set('key 1', 'value 1');
lru.set('key 2', 'value 2');
lru.set('key 3', 'value 3');
lru.set('key 4', 'value 4');

lru.keys(); // [ 'key 4', 'key 1', 'key 2', 'key 3']
// cache: {
//   'key 4': 'value 4',
// }
// _cache: {
//   'key 1': 'value 1',
//   'key 2': 'value 2',
//   'key 3': 'value 3',
// }

lru.reset()

reset a lru object.

const lru = new LRU(3);
lru.set('key 1', 'value 1');
lru.set('key 2', 'value 2');
lru.set('key 3', 'value 3');
lru.set('key 4', 'value 4');

lru.reset();
// cache: {
// }
// _cache: {
// }

lru.keys().length === 0;

License

MIT

Contributors


dominictarr


fengmk2


dead-horse


mourner


vagusX


RaoHai

This project follows the git-contributor spec, auto updated at Wed Mar 16 2022 23:57:13 GMT+0800.

FAQs

Last updated on 28 Mar 2024

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