@qelos/cache-manager 3.0.1 → 3.1.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
1
|
# @qelos/cache-manager
|
|
2
2
|
|
|
3
3
|
cache manager library for backend services, using redis or internal memory.
|
|
4
|
+
|
|
5
|
+
## Simple Installation Example
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
```bash
|
|
9
|
+
$ npm install @qelos/cache-manager
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
import {createCacheManager} from '@qelos/cache-manager';
|
|
14
|
+
import {redisUrl} from './config';
|
|
15
|
+
|
|
16
|
+
const cacher = redisUrl ?
|
|
17
|
+
require('@qelos/cache-manager/dist/redis-cache').createRedisCache(redisUrl) :
|
|
18
|
+
require('@qelos/cache-manager/dist/memory-cache').createMemoryCache()
|
|
19
|
+
|
|
20
|
+
const cacheManager = createCacheManager(cacher);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Get item from cache
|
|
24
|
+
```typescript
|
|
25
|
+
const value = await cacheManager.getItem('your-cache-key');
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Set item to cache
|
|
29
|
+
The value must in a string
|
|
30
|
+
```typescript
|
|
31
|
+
|
|
32
|
+
await cacheManager.setItem('your-cache-key', 'new value to store in cache');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Wrap a fallback to get an item
|
|
37
|
+
|
|
38
|
+
In this method, you can specify a key.
|
|
39
|
+
If the key doesn't exist inside the cache store, it will fall back to the function, return its value and store it inside the cache store.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const value = await cacheManager.wrap('your-cache-key', async () => {
|
|
43
|
+
const data = await someDatabase.query('select * from MyTable where id = X')
|
|
44
|
+
return JSON.stringify(data);
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Custom cache options
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
|
|
53
|
+
await cacheManager.setItem('your-cache-key', 'new value to store in cache', {
|
|
54
|
+
ttl: 1000 // in seconds
|
|
55
|
+
});
|
|
56
|
+
```
|