Coder Social home page Coder Social logo

lewibs-cache's Introduction

lewibs-cache

Overview

This utility allows you to convert a function into a caching function, enhancing performance by storing and retrieving its output based on specified options. This can be especially useful when you have computationally expensive functions that are repeatedly called with the same arguments.

If you need this to be more robust then you should just use react-query.

Installation

You can install this utility via npm:

npm install lewibs-cache

Usage

To use the caching utility, import it into your JavaScript code:

const cache = require('lewibs-cache');

//or 

import {cache} from "lewibs-cache";

Syntax

const cachedFunction = cache(func, options);

Parameters

  • func (function): The function you want to turn into a caching function.
  • options (Object): An optional object containing configuration options.

Options

  • key (Number): An integer value (0 to any number) indicating which parameter should be used to identify if the function should call from memory or run itself again. This parameter is optional and can be omitted if not needed.

Example

const expensiveCalculation = (a, b) => {
  console.log('Performing expensive calculation...');
  return a + b;
};

const cachedCalculation = cache(expensiveCalculation, { key: 0 });

console.log(cachedCalculation(2, 3)); // Calls the expensive calculation function and caches the result.
console.log(cachedCalculation(2, 3)); // Retrieves the result from the cache, avoiding a costly recalculation.

Contributing

Contributions are welcome! If you have suggestions, bug reports, or improvements, please open an issue or submit a pull request on the GitHub repository.

License

This utility is released under the MIT License. See the LICENSE file for more information.

lewibs-cache's People

Contributors

lewibs 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.