JSPM

bounded-cache

0.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q21167F
  • License MIT

A fast synchronous memory cache forgetting the least recently used entry when the maximal number of entries is reached

Package Exports

  • bounded-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bounded-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

A simple in memory cache forgetting the least recently accessed entry when the maximal number of entries is reached.

All operations are fast, O(1) and synchronous.

WARNING : this isn't yet officialy released and is only on github for peer-review !

This cache is mainly useful when

  • it's hard to predict what resources may be queried
  • queried resources are often queried again shortly after
  • you want to ensure the used memory is bounded

Examples

Example 1 - no invalidation, standard use case

Example 2 - with TTL based invalidation

Example 3 - with explicit invalidation of entries

var cache = require('bounded-cache')(20);

// this function is always called when the object is changed
function touch(key){
    cache.del(key);
}

function serve(key){
    var obj = 

}

API

set(key, value)

Sets a pair (key,value). If the key wasn't in the cache, it's considered to be the most recently accessed. If the cache is full, the least recently (key,value) is removed.

set(key, value, ttl)

Sets a pair (key,value) with an additionl validity duration in ms. If the key wasn't in the cache, it's considered to be the most recently accessed. If the cache is full, the least recently (key,value) is removed.

get(key)

Returns the value. The pair (key,value) is considered to be the most recently accessed. If nothing was set for this key, returns undefined.

peek(key)

Same as get without accessing the pair (and thus not preventing a removal from the cache).

del(key)

Removes the pair (key,value). Returns the value.

size()

Returns the number of cached keys, in [0, capacity].

content()

Returns all pairs (key,value), from the oldest to the last recently accessed. This operation is only here for test purposes.