.vscode | ||
test | ||
ts | ||
.gitignore | ||
.gitlab-ci.yml | ||
npmextra.json | ||
package.json | ||
pnpm-lock.yaml | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@push.rocks/smartcache
Cache things in smart ways.
Install
To use @push.rocks/smartcache
in your project, you'll need to install it using npm or yarn. Here's how you can do it:
npm install @push.rocks/smartcache --save
or if you are using yarn:
yarn add @push.rocks/smartcache
Usage
@push.rocks/smartcache
is designed to cache the results of function calls in a smart and efficient way, significantly improving the performance of repeat function calls by avoiding unnecessary recalculations or remote fetches. This package is especially useful when dealing with data that changes infrequently or expensive computation functions whose results can be reused within a given period. Here's how to use @push.rocks/smartcache
in your TypeScript projects:
First, make sure to import SmartCache
:
import { SmartCache } from '@push.rocks/smartcache';
Basic Example
Create an instance of SmartCache
:
const mySmartCache = new SmartCache();
Now, imagine you have an asynchronous function whose result you want to cache:
async function fetchData(): Promise<string> {
// simulate fetching data
await new Promise(resolve => setTimeout(resolve, 1000)); // delay to mimic fetch time
return 'Some fetched data';
}
Using @push.rocks/smartcache
, you can cache the result of fetchData
easily:
async function getCachedData() {
const cachedData = await mySmartCache.cacheReturn(fetchData, 60000); // Cache for 1 minute
console.log(cachedData); // 'Some fetched data'
}
Advanced Use Cases
Custom Cache Identifiers
By default, @push.rocks/smartcache
identifies cache entries based on the call stack. However, when you have dynamic arguments that significantly change the output of the function, you might want to create a custom cache identifier to differentiate between these calls.
Suppose fetchData
now takes an argument:
async function fetchData(userId: string): Promise<UserData> {
// Fetch user data based on userId
}
You could create a custom cache identifier like so:
const userId = '123';
const customIdentifier = `fetchData-${userId}`;
const cachedUserData = await mySmartCache.cacheReturn(() => fetchData(userId), 60000, customIdentifier);
Clearing the Cache
In certain situations, you might want to clear the cached data before it expires naturally. @push.rocks/smartcache
provides a method to manually clear specific cache entries:
mySmartCache.clearCache(customIdentifier);
Best Practices
- Cache Duration: Choose a cache duration that makes sense for your data. Highly dynamic data might not benefit much from long cache times, whereas static data can be cached longer.
- Error Handling: Always implement error handling for your cached functions, especially when dealing with network requests.
- Memory Management: Be mindful of what you cache. Caching large objects or a high number of entries can lead to increased memory usage.
Conclusion
@push.rocks/smartcache
is a powerful utility for caching asynchronous function results, significantly improving the performance and efficiency of your applications. By understanding and implementing the basics and exploring advanced use cases, you can leverage @push.rocks/smartcache
to its full potential.
Remember, the key to effective caching is understanding your data's behavior and aligning your caching strategy accordingly. Happy caching!
License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.
Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.