|
1 |
| -# Semantic Release Repo Template |
2 |
| -[](https://github.com/semantic-release/semantic-release) |
3 |
| -[](https://github.com/xojs/xo) |
4 |
| -[](../../actions/workflows/snyk-security.yml) |
5 |
| -[](../../actions/workflows/codeql.yml) |
6 |
| -[](https://securityscorecards.dev/viewer/?uri=github.com/tomerh2001/semantic-release-repo-template) |
7 |
| - |
8 |
| -# Configuration |
9 |
| -> [!NOTE] |
10 |
| -> Go to [Repository Secrets](../../settings/secrets/actions) settings and add the following: |
11 |
| -
|
12 |
| -| Name | Description | Required | |
13 |
| -| ------------------------- | ------------------------------------------ | -------- | |
14 |
| -| GH_TOKEN | Github Token | Yes | |
15 |
| -| NPM_TOKEN | NPM Token for publishing to NPM from CI/CD | Recommended | |
16 |
| -| CODECOV_TOKEN | Codecov Token for coverage tests | Recommended | |
17 |
| -| SNYK_TOKEN | Snyk Token for security tests | Recommended | |
18 |
| -| DOCKER_REGISTRY_USER | Docker registry user | Optional | |
19 |
| -| DOCKER_REGISTRY_PASSWORD | Docker registry password | Optional |
| 1 | +# cache-manager-function |
| 2 | + |
| 3 | +`cache-manager-function` is a vertical integration library that extends `cache-manager` to provide easy-to-use function-level caching with support for Redis and other stores. It allows you to cache function results with decorators, automatic cache initialization, and customizable cache key selection strategies. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Function Caching**: Easily cache async functions to improve performance. |
| 8 | +- **Class Method Caching**: Use decorators to add caching to class methods. |
| 9 | +- **Custom Key Selection**: Dynamically generate cache keys using functions, strings, or paths. |
| 10 | +- **TTL Management**: Set cache expiration times to keep your data fresh. |
| 11 | +- **Seamless Integration**: Built on top of `cache-manager` for reliable and configurable caching. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```shell |
| 16 | +npm install cache-manager-function cache-manager cache-manager-redis-store |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### 1. Initialize Cache Manager |
| 22 | + |
| 23 | +Before using caching, initialize the cache manager with your desired configuration. By default, it uses Redis as the cache store. |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { initializeCache } from 'cache-manager-function'; |
| 27 | + |
| 28 | +await initializeCache({ |
| 29 | + host: 'localhost', |
| 30 | + port: 6379, |
| 31 | + ttl: 60, // Time-to-live in seconds |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Caching Functions with `cacheFunction` |
| 36 | + |
| 37 | +Wrap your async functions with `cacheFunction` to automatically cache their results. |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { cacheFunction } from 'cache-manager-function'; |
| 41 | + |
| 42 | +async function fetchData(id) { |
| 43 | + // Fetch data logic |
| 44 | +} |
| 45 | + |
| 46 | +const cachedFetchData = cacheFunction(fetchData, { |
| 47 | + ttl: 120, |
| 48 | + keySelector: ['id'], |
| 49 | +}); |
| 50 | + |
| 51 | +const result = await cachedFetchData(123); |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Using `@cache` Decorator for Class Methods |
| 55 | + |
| 56 | +Use the `@cache` decorator to cache class methods with customizable TTL and key selection. |
| 57 | + |
| 58 | +```typescript |
| 59 | +import { cache } from 'cache-manager-function'; |
| 60 | + |
| 61 | +class DataService { |
| 62 | + @cache({ ttl: 180, keySelector: 'id' }) |
| 63 | + async getData(id) { |
| 64 | + // Fetch data logic |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const service = new DataService(); |
| 69 | +const data = await service.getData(123); |
| 70 | +``` |
| 71 | + |
| 72 | +### 4. Using `@cacheMeta` for Metadata |
| 73 | + |
| 74 | +The `@cacheMeta` decorator sets up metadata for caching, specifying how cache keys and TTLs are handled. This metadata can be used by other mechanisms to manage caching behavior. |
| 75 | + |
| 76 | +```typescript |
| 77 | +import { cacheMeta } from 'cache-manager-function'; |
| 78 | + |
| 79 | +class UserService { |
| 80 | + @cacheMeta({ ttl: 60, keySelector: ['userId'] }) |
| 81 | + async getUser(userId) { |
| 82 | + // Method logic |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## API |
| 88 | + |
| 89 | +### `initializeCache(config: CacheInitializationConfig): Promise<void>` |
| 90 | + |
| 91 | +Initializes the cache manager with the specified configuration. |
| 92 | + |
| 93 | +- **config**: Configuration object with optional `host`, `port`, `password`, and required `ttl`. |
| 94 | + |
| 95 | +### `cacheFunction(function_, options): Function` |
| 96 | + |
| 97 | +Wraps and caches an async function based on the provided options. |
| 98 | + |
| 99 | +- **function_**: The function to be cached. |
| 100 | +- **options**: Configuration options including `ttl` and `keySelector`. |
| 101 | + |
| 102 | +### `@cache(options: CacheOptions)` |
| 103 | + |
| 104 | +A decorator to cache class methods. |
| 105 | + |
| 106 | +- **options**: Configuration object with `ttl` and `keySelector`. |
| 107 | + |
| 108 | +### `@cacheMeta(options: CacheOptions)` |
| 109 | + |
| 110 | +Adds caching metadata to methods for defining cache keys and TTL without direct caching. |
| 111 | + |
| 112 | +- **options**: Configuration object with `ttl` and `keySelector`. |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
0 commit comments