npm.io
0.1.1 • Published 7 years ago

@byungi/lru-cache

Licence
MIT
Version
0.1.1
Deps
0
Size
7 kB
Vulns
0
Weekly
0
Stars
1

@byungi/lru-cache

Typed fast LRU cache for browser.

Implemented by the fast lru algorithm of hashlru. Added ttl and old browser support.

npm license

Install

npm i @byungi/lru-cache

Example

import LRUCache from '@byungi/lru-cache'

const cache = new LRUCache({max: 2, ttl: 100})

cache.set('a', 1)
console.log(cache.has('a')) // => true

cache.set('b', 2)
cache.set('c', 3)
cache.set('d', 4)
console.log(cache.has('a')) // => false

console.log(cache.has('d')) // => true
setTimeout(()=> {
    // After ttl(100ms).
    console.log(cache.has('d')) // => false
}, 100)

API

new LRUCache(options?)

Create an instance.

options
  • max - Maximum cache size. (Default: Infinity)
  • ttl - Time to live. (Default: Infinity)
  • renewTTL - If true, renew ttl when getting value. (Default: true)
cache.set(key, value)

Set value by key.

cache.get(key)

Get value by key.

cache.has(key)

Returns whether a key exists.

cache.delete(key)

Delete value by key

cache.clear()

Delete all values.

License

MIT

Keywords