0.1.1 • Published 5 years ago

@byungi/lru-cache v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

@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

0.1.1

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago