|
| 1 | +[](https://travis-ci.org/havsar/node-ts-cache) |
| 2 | +[](https://david-dm.org/havsar/node-ts-cache) |
| 3 | +[](https://www.npmjs.org/package/node-ts-cache) |
| 4 | +[](http://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +[](https://nodei.co/npm/node-ts-cache/) |
| 7 | + |
| 8 | +# labshare/services-cache |
| 9 | +Simple and extensible caching module with redis and memory storage and supporting decorators. |
| 10 | + |
| 11 | +<!-- TOC depthTo:2 --> |
| 12 | + |
| 13 | +- [node-ts-cache](#labshare/services-cache ) |
| 14 | +- [Install](#install) |
| 15 | +- [Usage](#usage) |
| 16 | + - [With decorator](#with-decorator) |
| 17 | + - [Directly](#directly) |
| 18 | +- [Strategies](#strategies) |
| 19 | + - [ExpirationStrategy](#expirationstrategy) |
| 20 | +- [Storages](#storages) |
| 21 | +- [Test](#test) |
| 22 | + |
| 23 | +<!-- /TOC --> |
| 24 | + |
| 25 | +# Install |
| 26 | +```bash |
| 27 | +npm install --save @labshare/services-cache |
| 28 | +``` |
| 29 | + |
| 30 | +# Usage |
| 31 | +## With decorator |
| 32 | +Caches function response using the given options. Works with different strategies and storages. Uses all arguments to build an unique key. |
| 33 | + |
| 34 | +`@Cache(options)` |
| 35 | +- `options`: Options passed to the strategy for this particular method |
| 36 | + |
| 37 | +*Note: @Cache always converts the method response to a promise because caching might be async.* |
| 38 | + |
| 39 | +```ts |
| 40 | +import { Cache, ExpirationStrategy, MemoryStorage } from "@labshare/services-cache"; |
| 41 | + |
| 42 | +class MyService { |
| 43 | + |
| 44 | + @Cache(myStrategy, { ttl: 60 }) |
| 45 | + public async getUsers(): Promise<string[]> { |
| 46 | + return ["John", "Doe"]; |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Directly |
| 52 | + |
| 53 | +```ts |
| 54 | +import { ExpirationStrategy, MemoryStorage } from "@labshare/services-cache"; |
| 55 | + |
| 56 | +const memoryCache = new ExpirationStrategy(new MemoryStorage()); |
| 57 | + |
| 58 | +class MyService { |
| 59 | + |
| 60 | + public async getUsers(): Promise<string[]> { |
| 61 | + const cachedUsers = await memoryCache.getItem<string[]>("users"); |
| 62 | + if (cachedUsers) { |
| 63 | + return cachedUsers; |
| 64 | + } |
| 65 | + |
| 66 | + const newUsers = ["John", "Doe"]; |
| 67 | + await memoryCache.setItem("users", newUsers, { ttl: 60 }); |
| 68 | + |
| 69 | + return newUsers; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +# Strategies |
| 75 | +## ExpirationStrategy |
| 76 | +Cached items expire after a given amount of time. |
| 77 | + |
| 78 | + - `ttl`: *(Default: 60)* Number of seconds to expire the cachte item |
| 79 | + - `isLazy`: *(Default: true)* If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given *ttl*. |
| 80 | + - `isCachedForver`: *(Default: false)* If true, cache entry has no expiration. |
| 81 | + - `refreshCache,`: boolean. |
| 82 | + - `noop?`: boolean; // Allows for consuming libraries to conditionally disable caching. Set this to true to disable caching for some reason. |
| 83 | + |
| 84 | + |
| 85 | +# Storages |
| 86 | + |
| 87 | +*Note: For specific storages, client libraries must be installed:* |
| 88 | + |
| 89 | +| Storage | Needed client library | |
| 90 | +|--------------|:---------------------:| |
| 91 | +| RedisStorage | `npm install redis` | |
| 92 | + |
| 93 | +#### MemoryStorage() |
| 94 | +#### FsJsonStorage(`fileName: string`) |
| 95 | +#### RedisStorage(`clientOpts:` [RedisClientOptions](https://github.com/NodeRedis/node_redis#options-object-properties)) |
| 96 | + |
| 97 | + |
| 98 | +# Test |
| 99 | +```bash |
| 100 | +npm test |
| 101 | +``` |
0 commit comments