https://github.com/mluby/lru-events
LRU cache with event hooks
https://github.com/mluby/lru-events
event-hooks library lru-cache
Last synced: 4 months ago
JSON representation
LRU cache with event hooks
- Host: GitHub
- URL: https://github.com/mluby/lru-events
- Owner: mLuby
- License: mit
- Created: 2016-12-08T08:25:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-12-08T11:17:40.000Z (over 8 years ago)
- Last Synced: 2025-01-28T11:09:18.403Z (4 months ago)
- Topics: event-hooks, library, lru-cache
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About
This is a **Least-Recently Used cache** with **event hooks** and an optional **expiry** mode that removes keys if they aren't used within the specified time.# Install
```shell
npm install --save lru-events
```
# Use
```js
var makeLRU = require("lru-events")
var lru = makeLRU()
var smallLru = makeLRU({max: 2})
var lruWithOptions = makeLRU({max: 2, expiryIfUnused: 5*60*1000})smallLru.set("a", "A")
smallLru.set("b", "B")
smallLru.get("a") // "A"
smallLru.set("c", "C")
smallLru.get("a") // "A"
smallLru.get("b") // undefined
smallLru.get("c") // "C"
```
# API
```haskell
clear :: () -> () -- does not clear event handlers; event callback takes no args.
get :: Key -> Value
length :: () -> Int -- event callback takes length.
on :: String -> (Key -> Value -> ()) -- callbacks generally take Key and Value args.
peek :: Key -> Value -- does not change recently used order or expiry times
remove :: Key -> Value
set :: Key -> Value -> Value
```# Defaults
```js
options = {
max: 1000, // number of items before the least-recently used gets evicted.
expiryIfUnused: Infinity // in milliseconds
}
```