Skip to content

skt-t1-byungi/lru-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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

About

Typed, fast LRU cache for browser.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published