Coder Social home page Coder Social logo

hurryhuang1007 / fn-merge-cache Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 182 KB

`FnMergeCache` is a caching utility that allows functions to cache their results based on input arguments, with options for cache lifetime, size limits, error handling, and parameter comparison, while supporting cache invalidation via tags and global revalidation.

License: MIT License

TypeScript 100.00%
cache function-cache lru memoize optimization performance promise-cache ttl tag-based-cache

fn-merge-cache's Introduction

fn-merge-cache

FnMergeCache is a caching utility that allows functions to cache their results based on input arguments, with options for cache lifetime, size limits, error handling, and parameter comparison, while supporting cache invalidation via tags and global revalidation.

NPM version NPM downloads 中文文档

Install

npm install fn-merge-cache

Features

  1. Cache function: supports caching the return results of functions to improve performance and reduce repeated calculations.
  2. Cache expiration: supports setting the expiration time (TTL) of the cache, and automatically clears the cache after the time expires.
  3. Cache size limit: supports setting the maximum cache size to limit the size of the cache pool.
  4. Error handling: you can choose whether to cache the error result when the function call fails.
  5. Parameter comparison: allows custom parameter comparison functions to determine whether the cached results can be used.
  6. Cache invalidation: cache invalidation and revalidation (revalidation) through the tag mechanism, you can revalidate the cache by tag or globally.
  7. Merge promises: support processing the returned Promise, and update the cache status after the Promise is resolved.
  8. Event-driven: use EventEmitter for cache invalidation notification, allowing fine control over the validity of the cache.
  9. Intelligent caching strategy: When the cache TTL is not set, the LRU algorithm will be used.

API

FnMergeCache

A tool for caching function call results, supporting cache expiration, size limits, error handling, tag mechanisms, and more.

Constructor

new FnMergeCache<A extends any[], R>(
  fn: (...args: A) => R,
  opt?: {
    cache?: boolean;
    cacheOnError?: boolean;
    argComparer?: (a: A, b: A) => boolean;
    ttl?: number;
    maxCacheSize?: number;
    tags?: string[];
  }
)

Parameters

  • fn: The function to be cached.
  • cache: Whether to enable caching. Default is true.
  • cacheOnError: Whether to cache the result even if the function throws an error. Default is false.
  • argComparer: A custom function to compare function arguments. Defaults to lodash's isEqual.
  • ttl: The cache time-to-live (in milliseconds), 0 means never expires. Default is 0.
  • maxCacheSize: The maximum number of cached items, 0 means no limit. Default is 0.
  • tags: Tags for cache invalidation.

Methods

  • call(...args: A): R

    Calls the cached function with the provided arguments args, returning the result. If a valid cached result exists, it returns the cached result; otherwise, it executes the function and caches the result.

  • revalidate()

    Clears all cached data. This method clears all cache data related to the instance.

  • dispose()

    Releases the cache instance resources, stops caching, and removes all event listeners.

createMergedCachedFn

Creates a function with caching functionality.

function createMergedCachedFn<A extends any[], R>(
  fn: (...args: A) => R,
  opt?: {
    cache?: boolean;
    cacheOnError?: boolean;
    argComparer?: (a: A, b: A) => boolean;
    ttl?: number;
    maxCacheSize?: number;
    tags?: string[];
  }
): (...args: A) => R

Parameters

  • fn: The function to be cached.
  • opt: Cache options, same as the options for FnMergeCache constructor.

Returns

A function with caching functionality, behaving the same as FnMergeCache.

revalidateTag

Revalidate the cache by tag(s) or globally.

function revalidateTag(tags?: string | string[]): void

Basic Usage

import { FnMergeCache, revalidateTag } from 'fn-merge-cache';

// define a function that needs to be cached
function fetchData(id) {
  console.log(`Fetching data for ${id}`);
  return new Promise((resolve) => {
    setTimeout(() => resolve(`Data for ${id}`), 1000);
  });
}

// create a cache instance
const cache = new FnMergeCache(fetchData, {
  cache: true, // enable cache
  cacheOnError: true, // cache error results
  ttl: 5000, // cache expiration time
  maxCacheSize: 10, // maximum cache size
  tags: ['data-fetch'], // cache tags
});

// call the function with the same parameter
cache.call(1).then((data) => {
  console.log(data); // Output: Fetching data for 1 \n Data for 1
});
cache.call(1).then((data) => {
  console.log(data); // Output: Data for 1 (from cache)
});

// call the function with different parameters
cache.call(2).then((data) => {
  console.log(data); // Output: Fetching data for 2 \n Data for 2
});

// revalidate the cache
cache.revalidate();

// or revalidate the cache by tag(s)
revalidateTag('data-fetch');

// or revalidate all caches
revalidateTag();

// call the function with the same parameter
cache.call(1).then((data) => {
  console.log(data); // Output: Fetching data for 1 \n Data for 1 (fetch again)
});

// dispose the cache instance
cache.dispose();

if a cache instance DO NOT need to be disposed, you can use the createMergedCachedFn function

import { createMergedCachedFn, revalidateTag } from 'fn-merge-cache';

const mergedCachedFn = createMergedCachedFn(fetchData, {
  cache: true,
  cacheOnError: true,
  ttl: 5000,
  maxCacheSize: 10,
  tags: ['data-fetch'],
});

// call the function with the same parameter
mergedCachedFn(1).then((data) => {
  console.log(data); // Output: Fetching data for 1 \n Data for 1
});

// revalidate the cache
revalidateTag('data-fetch');

Merge Usage (only when the fn returns a Promise (async function))

import { createMergedCachedFn } from 'fn-merge-cache';

const justMergeFn = createMergedCachedFn(fetchData, {
  cache: false,
});

justMergeFn(1).then((data) => {
  console.log(data); // Output: Fetching data for 1 \n Data for 1
});
justMergeFn(1).then((data) => {
  console.log(data); // Output: Data for 1 (merged call)

  justMergeFn(1).then((data) => {
    console.log(data); // Output: Fetching data for 1 \n Data for 1 (fetch again)
  });
});

LICENSE

MIT

fn-merge-cache's People

Contributors

hurryhuang1007 avatar

Stargazers

Howl avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.