A module for caching asynchronous function results with smart time and hash-based invalidation strategies.
Go to file
2024-04-14 17:23:41 +02:00
.vscode fix(core): update 2022-06-26 20:36:22 +02:00
test fix(core): update 2022-06-26 20:36:22 +02:00
ts fix(core): update 2023-01-08 09:18:41 +01:00
.gitignore fix(core): update 2021-04-21 19:52:53 +00:00
.gitlab-ci.yml fix(core): update 2022-06-26 20:36:22 +02:00
npmextra.json update tsconfig 2024-04-14 17:23:41 +02:00
package.json update tsconfig 2024-04-14 17:23:41 +02:00
pnpm-lock.yaml switch to new org scheme 2023-07-11 00:17:21 +02:00
readme.hints.md update tsconfig 2024-04-14 17:23:41 +02:00
readme.md update tsconfig 2024-04-14 17:23:41 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:34:01 +02:00

@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!

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.