Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The ylru package is a simple and efficient LRU (Least Recently Used) cache implementation in JavaScript. It is designed to help manage a cache of items with a fixed size, automatically removing the least recently used items when the cache exceeds its capacity. This is particularly useful in applications where memory management and data retrieval efficiency are critical.
Create and manage an LRU cache
This feature allows the user to create a cache with a specified limit and perform basic operations such as setting, getting, checking, and deleting items, as well as retrieving the size of the cache.
const YLRU = require('ylru');
let cache = new YLRU(100); // Create a cache that can hold 100 items
// Set items in the cache
cache.set('key1', 'value1');
cache.set('key2', 'value2');
// Get an item from the cache
let value = cache.get('key1'); // returns 'value1'
// Check if a key exists in the cache
let exists = cache.has('key1'); // returns true
// Remove an item from the cache
cache.delete('key2');
// Get the number of items in the cache
let size = cache.size(); // returns 1
lru-cache is another popular LRU cache implementation. It offers additional features such as item expiration based on time, which ylru does not support. This makes lru-cache suitable for scenarios where cache items should expire after a certain duration.
node-cache is a more general caching library that supports both LRU mechanism and other types of cache strategies. Unlike ylru, which is specifically focused on LRU, node-cache provides a more flexible solution for various caching needs.
hashlru inspired
hashlru is the Simpler, faster LRU cache algorithm. Please checkout algorithm and complexity on hashlru.
ylru extends some features base on hashlru:
null
, undefined
, ''
, 0
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 });
initialize a lru object.
{Number} options.maxAge
: update expire time when get, value will become undefined
after maxAge
pass.Returns the value in the cache.
{Number} options.maxAge
: value will become undefined
after maxAge
pass.
If maxAge
not set, value will be never expired.Set the value for key.
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',
// }
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
.
1.4.0 (2024-03-28)
others
eda13ec
] - test: update toolchains, add tsd test cases (raohai.rh <raohai.rh@antgroup.com>)b683526
] - 🤖 TEST: Create codeql-analysis.yml (#7) (fengmk2 <fengmk2@gmail.com>)db06032
] - 🤖 TEST: Run on github action (#6) (fengmk2 <fengmk2@gmail.com>)fixes
18f576e
] - fix: module declares (#5) (陆沉 <surgesoft@gmail.com>)features
6ed8fa0
] - feat: add d.ts (#4) (vagusX <vagusX@users.noreply.github.com>)others
475abb0
] - perf: only call Date.now() when necessary (#3) (Yiyu He <dead_horse@qq.com>)FAQs
Extends LRU base on hashlru
The npm package ylru receives a total of 1,984,993 weekly downloads. As such, ylru popularity was classified as popular.
We found that ylru demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.