Skip to content

fatcherjs/middleware-cache

Repository files navigation

@fatcherjs/middleware-cache

Install

NPM

>$ npm install @fatcherjs/middleware-cache

CDN

<script src="https://cdn.jsdelivr.net/npm/fatcher/dist/fatcher.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-cache/dist/index.min.js"></script>

<script>
  Fatcher.fatcher('url', {
    middlewares: [FatcherMiddlewareCache.cache],
    ttl: 60 * 1000, // 60s
  }).then(async response => {
    const text = await response.text();

    Fatcher.fatcher('url', {
      middlewares: [FatcherMiddlewareCache.cache],
    }).then(async response2 => {
      const text2 = await response2.text();

      console.log(text === text2); // true
    });
  });
</script>

Usage

import { fatcher } from 'fatcher';
import { cache } from '@fatcherjs/middleware-cache';

fatcher('https://foo.bar', {
  middlewares: [cache],
});

Options

ttl

import { fatcher } from 'fatcher';
import { cache } from '@fatcherjs/middleware-cache';

fatcher('https://foo.bar', {
  ttl: 5 * 1000,
  middlewares: [cache],
});

flush

import { fatcher } from 'fatcher';
import { cache } from '@fatcherjs/middleware-cache';

fatcher('https://foo.bar', {
  ttl: 5 * 1000,
  middlewares: [cache],
});

fatcher('https://foo.bar', {
  ttl: 5 * 1000,
  flush: true, // ignore cache and refresh cache with ttl > 0
  middlewares: [cache],
});

API

clearCache

import { fatcher } from 'fatcher';
import { cache, clearCache } from '@fatcherjs/middleware-cache';

fatcher('https://foo.bar', {
  ttl: 5 * 1000,
  middlewares: [cache],
});

clearCache('https://foo.bar'); // clear single cache
clearCache(); // clear all cache

License

MIT

About

@fatcherjs/middleware-cache

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages