@midwayjs/cache-manager 3.14.0 → 3.14.3

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/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from './interface';
3
3
  export * from './factory';
4
4
  export * from './decorator/cacheKey';
5
5
  export * as CacheManager from 'cache-manager';
6
+ export * from './store';
6
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -22,4 +22,5 @@ __exportStar(require("./interface"), exports);
22
22
  __exportStar(require("./factory"), exports);
23
23
  __exportStar(require("./decorator/cacheKey"), exports);
24
24
  exports.CacheManager = require("cache-manager");
25
+ __exportStar(require("./store"), exports);
25
26
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ import type { Config, Store } from 'cache-manager';
2
+ import { IMidwayContainer } from '@midwayjs/core';
3
+ export interface RedisStore extends Store {
4
+ readonly isCacheable: (value: unknown) => boolean;
5
+ }
6
+ export declare function createRedisStore(instanceName: string): (options: Config, container: IMidwayContainer) => Promise<RedisStore>;
7
+ //# sourceMappingURL=store.d.ts.map
package/dist/store.js ADDED
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRedisStore = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ const getVal = (value) => JSON.stringify(value) || '"undefined"';
6
+ function createRedisStore(instanceName) {
7
+ return async (options, container) => {
8
+ const { RedisServiceFactory } = (0, core_1.safeRequire)('@midwayjs/redis');
9
+ const redisServiceFactory = await container.getAsync(RedisServiceFactory);
10
+ const redisInstance = redisServiceFactory.get(instanceName);
11
+ return createStore(redisInstance, options);
12
+ };
13
+ }
14
+ exports.createRedisStore = createRedisStore;
15
+ function createStore(redisCache, options) {
16
+ const isCacheable = (options === null || options === void 0 ? void 0 : options.isCacheable) || (value => value !== undefined && value !== null);
17
+ const keys = (pattern) => redisCache.keys(pattern);
18
+ return {
19
+ async get(key) {
20
+ const val = await redisCache.get(key);
21
+ if (val === undefined || val === null)
22
+ return undefined;
23
+ else
24
+ return JSON.parse(val);
25
+ },
26
+ async set(key, value, ttl) {
27
+ if (!isCacheable(value))
28
+ throw new core_1.MidwayCommonError(`"${value}" is not a cacheable value`);
29
+ const t = ttl === undefined ? options === null || options === void 0 ? void 0 : options.ttl : ttl;
30
+ if (t !== undefined && t !== 0)
31
+ await redisCache.set(key, getVal(value), 'PX', t);
32
+ else
33
+ await redisCache.set(key, getVal(value));
34
+ },
35
+ async mset(args, ttl) {
36
+ const t = ttl === undefined ? options === null || options === void 0 ? void 0 : options.ttl : ttl;
37
+ if (t !== undefined && t !== 0) {
38
+ const multi = redisCache.multi();
39
+ for (const [key, value] of args) {
40
+ if (!isCacheable(value))
41
+ throw new core_1.MidwayCommonError(`"${getVal(value)}" is not a cacheable value`);
42
+ multi.set(key, getVal(value), 'PX', t);
43
+ }
44
+ await multi.exec();
45
+ }
46
+ else
47
+ await redisCache.mset(args.flatMap(([key, value]) => {
48
+ if (!isCacheable(value))
49
+ throw new Error(`"${getVal(value)}" is not a cacheable value`);
50
+ return [key, getVal(value)];
51
+ }));
52
+ },
53
+ mget: (...args) => redisCache
54
+ .mget(args)
55
+ .then(x => x.map(x => x === null || x === undefined
56
+ ? undefined
57
+ : JSON.parse(x))),
58
+ async mdel(...args) {
59
+ await redisCache.del(args);
60
+ },
61
+ async del(key) {
62
+ await redisCache.del(key);
63
+ },
64
+ ttl: async (key) => redisCache.pttl(key),
65
+ keys: (pattern = '*') => keys(pattern),
66
+ reset: () => {
67
+ throw new core_1.MidwayCommonError('flushdb() is too dangerous, if necessary, please use redisServiceFactory.get(client) to get the instance and call it manually.');
68
+ },
69
+ isCacheable,
70
+ };
71
+ }
72
+ //# sourceMappingURL=store.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/cache-manager",
3
- "version": "3.14.0",
3
+ "version": "3.14.3",
4
4
  "description": "midway cache manager",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -28,14 +28,14 @@
28
28
  "node": ">=12"
29
29
  },
30
30
  "devDependencies": {
31
- "@midwayjs/core": "^3.14.0",
32
- "@midwayjs/mock": "^3.14.0",
33
- "@midwayjs/redis": "^3.14.0",
31
+ "@midwayjs/core": "^3.14.3",
32
+ "@midwayjs/mock": "^3.14.3",
33
+ "@midwayjs/redis": "^3.14.3",
34
34
  "cache-manager-ioredis-yet": "1.2.2"
35
35
  },
36
36
  "dependencies": {
37
37
  "cache-manager": "5.3.2",
38
38
  "promise-coalesce": "1.1.1"
39
39
  },
40
- "gitHead": "67e3a3596ffdb65a413064d30c68f1f7965af282"
40
+ "gitHead": "7517e708722bbf9ba69d9406e73fa2597e485d24"
41
41
  }