@varasto/cache-storage
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@varasto/cache-storage

npm

Implementation of an storage which acts as an cache for another storage.

Installation

$ npm install --save @varasto/cache-storage

Usage

The package provides an function called createCacheStorage which takes another Storage instance as an argument, and optional argument which defines TTL in milliseconds for cached entries. If the TTL argument is omitted, entries are cached indefinitely.

import { createCacheStorage } from '@varasto/cache-storage';
import { createRemoteStorage } from '@varasto/remote-storage';

const remoteStorage = createRemoteStorage({ url: 'https://example.com' });
// Time to live for cached entries is one minute, or 60000 milliseconds.
const cacheStorage = createCacheStorage(remoteStorage, 60 * 1000);

await remoteStorage.set('foo', 'bar', { value: 5 });

// Entry is not cached yet, so it's retrieved from the remote storage first.
await cacheStorage.get('foo', 'bar');

// This time the entry should come from the cache and not remote storage.
await cacheStorage.get('foo', 'bar');

setTimeout(async () => {
  // Previously cached entry should been expired now, so it's retrieved from
  // the remote storage and cached again.
  await cacheStorage.get('foo', 'bar');
}, 60 * 1000);

Readme

Keywords

Package Sidebar

Install

npm i @varasto/cache-storage

Weekly Downloads

6

Version

3.0.0

License

MIT

Unpacked Size

14.8 kB

Total Files

10

Last publish

Collaborators

  • rauli