Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ylru

node-modules13.3mMIT2.0.0TypeScript support: included

Extends LRU base on hashlru

readme

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

import { LRU } from '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

new 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


fengmk2


dominictarr


dead-horse


thonatos


mourner


semantic-release-bot


vagusX


RaoHai

This project follows the git-contributor spec, auto updated at Thu Mar 28 2024 11:52:18 GMT+0800.

changelog

Changelog

2.0.0 (2024-06-22)

⚠ BREAKING CHANGES

  • drop Node.js < 18.19.0 support

https://github.com/eggjs/egg/issues/5257

Summary by CodeRabbit

  • New Features
  • Introduced a Least Recently Used (LRU) cache implementation with enhanced functionalities.

  • Bug Fixes

    • Improved cache item expiration handling and test case accuracy.
  • Documentation

    • Updated README with new import syntax and method signatures.
    • LICENSE changed to MIT License.
  • Chores

  • Updated ESLint configuration, Node.js version in workflows, and .gitignore.

  • Refactor

  • Converted test cases to use async/await and updated module imports to ES module syntax.

  • Dependencies

    • Updated devDependencies and added new scripts in package.json.
  • Build Configuration

    • Updated tsconfig.json for stricter typing and ES2022 target.

Features

1.4.0 (2024-03-28)

Features


1.3.2 / 2022-03-17

others

1.3.1 / 2022-03-16

fixes

1.3.0 / 2022-03-16

features

1.2.1 / 2018-07-11

others

1.2.0 / 2017-07-18

  • feat: support lru.keys (#2)

1.1.0 / 2017-07-04

  • feat: support get with maxAge (#1)

1.0.0 / 2016-12-29

  • init version