https://github.com/jaebradley/simple-lru-cache
A(nother) Simple LRU Cache 💸
https://github.com/jaebradley/simple-lru-cache
lru-cache node-module nodejs npm npm-package
Last synced: about 1 month ago
JSON representation
A(nother) Simple LRU Cache 💸
- Host: GitHub
- URL: https://github.com/jaebradley/simple-lru-cache
- Owner: jaebradley
- License: mit
- Created: 2018-06-24T02:37:21.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-02T18:38:47.000Z (about 6 years ago)
- Last Synced: 2025-10-28T22:35:04.905Z (7 months ago)
- Topics: lru-cache, node-module, nodejs, npm, npm-package
- Language: JavaScript
- Homepage:
- Size: 767 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://greenkeeper.io/)
[](https://travis-ci.org/jaebradley/simple-lru-cache)
[](https://codecov.io/gh/jaebradley/simple-lru-cache)
[](https://www.npmjs.com/package/@jaebradley/simple-lru-cache)

[](https://www.npmjs.com/package/@jaebradley/simple-lru-cache)

# `@jaebradley/simple-lru-cache`
A simple Least Recently Used `(LRU)` Cache implemented mostly as an exercise.
The goal was to create a simple API with limited surface area to keep private variables and methods, private.
## Installation
```bash
npm i @jaebradley/simple-lru-cache --save
```
## API
### Import
```javascript
import LRUCache from '@jaebradley/simple-lru-cache';
```
### Cache Creation
**Note: The `LRUCache` function is NOT a `constructor` - do not use the `new` operator**
The `LRUCache` function takes a single `maximumSize` parameter that specifies the maximum number of entries the cache can hold. The `maximumSize` parameter must be an integer.
```javascript
const cache = LRUCache(); // cache with a maximum size of 10, by default
const smallCache = LRUCache(1); // cache with a maximum size of 1
const largeCache = LRUCache(1000000); // cache with a maximum size of one million
```
### `clear`
Clears all cache entries.
```javascript
const cache = LRUCache(123);
cache.set({ key: 'key', value: 'value' });
cache.isEmpty() // false
cache.clear();
cache.isEmpty() // true
```
### `get`
Gets the `value` associated with a cached `key`. If the specified `key` is not found within the cache, `undefined` is returned.
```javascript
const cache = LRUCache(123);
cache.set({ key: 'key', value: 'value' });
cache.get('key'); // 'value'
cache.get('foobar'); // undefined
```
### `getLeastRecentlyUsedEntry`
Returns the `key` and `value` that was least recently used. Will return `undefined` if no entry exists (i.e. when the cache is empty).
```javascript
const cache = LRUCache(123);
cache.getLeastRecentlyUsedEntry(); // undefined
cache.set({ key: 'key', value: 'value' });
cache.set({ key: 'anotherKey', value: 'anotherValue' });
cache.getLeastRecentlyUsedEntry(); // { key: 'key', value: 'value' }
```
### `getMaximumSize`
Gets the maximum number of cache entries which was specified at cache instantiation.
```javascript
const cache = LRUCache(123);
cache.getMaximumSize(); // 123
```
### `getMostRecentlyUsedEntry`
Returns the `key` and `value` that was most recently used. Will return `undefined` if no entry exists (i.e. when the cache is empty).
```javascript
const cache = LRUCache(123);
cache.getMostRecentlyUsedEntry(); // undefined
cache.set({ key: 'key', value: 'value' });
cache.set({ key: 'anotherKey', value: 'anotherValue' });
cache.getMostRecentlyUsedEntry(); // { key: 'anotherKey', value: 'anotherValue' }
```
### `getSize`
Gets the current size of the cache.
```javascript
const cache = LRUCache(123);
cache.getSize(); // 0
cache.set({ key: 'key', value: 'value' });
cache.getSize(); // 1
```
### `isEmpty`
Convenience method that returns `true` if cache is empty (i.e. the size of the cache is `0`) and `false` if the cache is not empty.
```javascript
const cache = LRUCache(123);
cache.isEmpty(); // true
cache.set({ key: 'key', value: 'value' });
cache.isEmpty(); //false
```
### `set`
Cache a `value` that is associated with a `key`. If the cache is at the maximum size, and a new entry is added to the cache, the least recently used entry will be evicted from the cache.
```javascript
const cache = LRUCache(1);
cache.set({ key: 'key', value: 'value' });
cache.get('key'); // 'value'
cache.set({ key: 'anotherKey', value: 'anotherValue' });
cache.get('key'); // undefined
cache.get('anotherKey'); // 'anotherValue'
```
### `remove`
Remove an entry from the cache.
```javascript
const cache = LRUCache(123);
cache.set({ key: 'key', value: 'value' });
cache.get('key'); // value
cache.remove('key');
cache.get('key'); // undefined
```