@qelos/cache-manager 3.0.1 → 3.2.0

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 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
+ ```
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CacheManagerOptions } from "./cache-manager-options";
2
2
  import { ICache } from "./cache-type";
3
- export declare type IDataProviderCallback = () => string | Promise<string>;
3
+ export type IDataProviderCallback = () => string | Promise<string>;
4
4
  export interface CacheManager {
5
5
  getItem: (key: string) => Promise<string>;
6
6
  setItem: (key: string, value: string, { ttl }?: CacheManagerOptions) => Promise<void>;
@@ -4,7 +4,7 @@ exports.createRedisCache = void 0;
4
4
  const util_1 = require("util");
5
5
  const redis_1 = require("redis");
6
6
  function createRedisCache(redisUrl) {
7
- const client = (0, redis_1.createClient)(redisUrl);
7
+ const client = (0, redis_1.createClient)({ url: redisUrl });
8
8
  client.on("connect", function () {
9
9
  console.log('redis is connected for cache manager');
10
10
  });
@@ -1 +1 @@
1
- {"version":3,"file":"redis-cache.js","sourceRoot":"","sources":["../redis-cache.ts"],"names":[],"mappings":";;;AACA,+BAAiC;AACjC,iCAAqC;AAGrC,SAAgB,gBAAgB,CAAC,QAAgB;IAC7C,MAAM,MAAM,GAAG,IAAA,oBAAY,EAAC,QAAQ,CAAC,CAAC;IACtC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE;QACjB,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK;QAC9B,OAAO,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,IAAA,gBAAS,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACnD,MAAM,OAAO,GAAG,IAAA,gBAAS,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEnD,OAAO;QACH,OAAO;QACP,KAAK,CAAC,OAAO,CAAC,GAAW,EAAE,KAAa,EAAE,EAAE,GAAG,EAAuB;YAClE,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC1B,IAAI,GAAG,EAAE;gBACL,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;aAC3B;QACL,CAAC;KACJ,CAAC;AACN,CAAC;AArBD,4CAqBC"}
1
+ {"version":3,"file":"redis-cache.js","sourceRoot":"","sources":["../redis-cache.ts"],"names":[],"mappings":";;;AACA,+BAAiC;AACjC,iCAAqC;AAGrC,SAAgB,gBAAgB,CAAC,QAAgB;IAC7C,MAAM,MAAM,GAAG,IAAA,oBAAY,EAAC,EAAC,GAAG,EAAE,QAAQ,EAAC,CAAC,CAAC;IAC7C,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE;QACjB,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK;QAC9B,OAAO,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,IAAA,gBAAS,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACnD,MAAM,OAAO,GAAG,IAAA,gBAAS,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEnD,OAAO;QACH,OAAO;QACP,KAAK,CAAC,OAAO,CAAC,GAAW,EAAE,KAAa,EAAE,EAAE,GAAG,EAAuB;YAClE,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC1B,IAAI,GAAG,EAAE;gBACL,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;aAC3B;QACL,CAAC;KACJ,CAAC;AACN,CAAC;AArBD,4CAqBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qelos/cache-manager",
3
- "version": "3.0.1",
3
+ "version": "3.2.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "private": false,
@@ -11,11 +11,11 @@
11
11
  "author": "",
12
12
  "license": "MIT",
13
13
  "dependencies": {
14
- "redis": "3.0.2"
14
+ "redis": "4.6.5"
15
15
  },
16
16
  "devDependencies": {
17
- "@types/redis": "^2.8.31",
18
- "typescript": "^4.6.3"
17
+ "@types/redis": "^4.0.11",
18
+ "typescript": "^4.9.5"
19
19
  },
20
20
  "publishConfig": {
21
21
  "access": "public"
package/redis-cache.ts CHANGED
@@ -4,7 +4,7 @@ import { createClient } from 'redis';
4
4
  import { CacheManagerOptions } from "./cache-manager-options";
5
5
 
6
6
  export function createRedisCache(redisUrl: string): ICache {
7
- const client = createClient(redisUrl);
7
+ const client = createClient({url: redisUrl});
8
8
  client.on("connect", function () {
9
9
  console.log('redis is connected for cache manager');
10
10
  });