lewibs-cache
v0.0.1
Published
This is a library used to quickly turn functions into cachable functions
Downloads
1
Readme
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.