@specialblend/cash

functional, selector-based wrapper around lru-cache

Usage no npm install needed!

<script type="module">
  import specialblendCash from 'https://cdn.skypack.dev/@specialblend/cash';
</script>

README

cash

functional, selector-based wrapper around lru-cache

install

npm install @specialblend/cash

usage

import Cash from '@specialblend/cash';

async function fetchTodoById (id) {
    // IO intensive operation ...
}

const todoCache = new Cash(fetchTodoById)

async function main() {
    await todoCache.resolve('test-todo-id'); // => calls fetchTodoById with test-todo-id and caches the result

    await todoCache.resolve('test-todo-id'); // => returns the cached result

    await todoCache.resolve('fooBarId'); // => calls fetchTodoById with fooBarId and caches the result

    await todoCache.resolve('fooBarId'); // => returns the cached result
}

philosphy

Presumably, when you ask a traditional LRU cache if it has a given key, you're not doing it for fun.

If the key exists, you want its value. If it does not, you want to run a long (async) operation and tell the cache to remember it for next time.

As far as you're concerned, this process is linear. Why not make it functional (well, to the extent that a Promise can be considered a monad)?