https://github.com/ceramicnetwork/least-recent
A cache object that deletes the least-recently-used items
https://github.com/ceramicnetwork/least-recent
Last synced: 11 months ago
JSON representation
A cache object that deletes the least-recently-used items
- Host: GitHub
- URL: https://github.com/ceramicnetwork/least-recent
- Owner: ceramicnetwork
- Created: 2023-08-14T17:40:29.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-14T17:43:50.000Z (almost 3 years ago)
- Last Synced: 2025-03-07T08:36:36.166Z (over 1 year ago)
- Language: TypeScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# most-recent
Provides an implementation of LRUCache. Effectively a fork of [LRUCache from Mnemonist](https://github.com/Yomguithereal/mnemonist/tree/master) with one additional feature.
`LRU` standing for _least recently used_, can be seen as a a fixed-capacity key-value store that will evict infrequent items when full and setting new keys.
For instance, if one creates a `LRUCache` with a capacity of `1000` and one inserts a thousand-and-first key, the cache will forget its least recently used key-value pair in order not to overflow the allocated memory.
This structure is very useful to cache the result of costly operations when one cannot afford to keep every result in memory and only want to keep the most frequent ones.
For more information, you can check [this]() Wikipedia page.
```typescript
import { LRUCache } from "most-recent";
```
## Usage
The `LRUCache` takes a single argument to create: the desired capacity. You could also provide types for keys and values.
```typescript
const cache = new LRUCache(1000);
const stringCache = new LRUCache(1000);
```
For available methods, please see [Mnemonist LRUCache documentation](https://yomguithereal.github.io/mnemonist/lru-cachehttps://yomguithereal.github.io/mnemonist/lru-cache)
The only difference from Mnemonist, is we emit an event (using [`nanoevents`](https://www.npmjs.com/package/nanoevents)) if an entry gets evicted.
```typescript
import { LRUCache } from "most-recent";
const cache = new LRUCache(1000);
cache.on("evicted", (key, value) => {
console.log("evicted entry", key, value);
});
```