@autofleet/matmon 1.0.0 → 1.0.2-beta-1

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/lib/index.d.ts CHANGED
@@ -1,11 +1,3 @@
1
- declare const _default: {
2
- getNewLRU: (lifeTimeInSec: any, size: any) => any;
3
- getWithCacheSupport: ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }: {
4
- cacheKey: any;
5
- cacheGet: any;
6
- cacheSet: any;
7
- fetching: any;
8
- skipCache: any;
9
- }) => Promise<any>;
10
- };
11
- export default _default;
1
+ import { getNewLRU, getWithCacheSupport } from './cache';
2
+ import RedisCache from './redis';
3
+ export { getNewLRU, getWithCacheSupport, RedisCache, };
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RedisCache = exports.getWithCacheSupport = exports.getNewLRU = void 0;
3
7
  const cache_1 = require("./cache");
4
- exports.default = {
5
- getNewLRU: cache_1.getNewLRU,
6
- getWithCacheSupport: cache_1.getWithCacheSupport,
7
- };
8
+ Object.defineProperty(exports, "getNewLRU", { enumerable: true, get: function () { return cache_1.getNewLRU; } });
9
+ Object.defineProperty(exports, "getWithCacheSupport", { enumerable: true, get: function () { return cache_1.getWithCacheSupport; } });
10
+ const redis_1 = __importDefault(require("./redis"));
11
+ exports.RedisCache = redis_1.default;
@@ -0,0 +1,13 @@
1
+ import { RedisError } from 'redis';
2
+ declare class RedisCacheError extends RedisError {
3
+ innerError: any;
4
+ message: string;
5
+ constructor(msg: string, err: any);
6
+ toString(): string;
7
+ }
8
+ declare class RedisLockError extends RedisCacheError {
9
+ key: string;
10
+ constructor(msg: string, err: any, key: string);
11
+ toString(): string;
12
+ }
13
+ export { RedisCacheError, RedisLockError, };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisLockError = exports.RedisCacheError = void 0;
4
+ const redis_1 = require("redis");
5
+ class RedisCacheError extends redis_1.RedisError {
6
+ constructor(msg, err) {
7
+ super();
8
+ this.innerError = err;
9
+ this.message = msg;
10
+ }
11
+ toString() {
12
+ return `${this.message}: ${this.innerError}`;
13
+ }
14
+ }
15
+ exports.RedisCacheError = RedisCacheError;
16
+ class RedisLockError extends RedisCacheError {
17
+ constructor(msg, err, key) {
18
+ super(msg, err);
19
+ this.key = key;
20
+ }
21
+ toString() {
22
+ return `${this.message} (${this.key}): ${this.innerError}`;
23
+ }
24
+ }
25
+ exports.RedisLockError = RedisLockError;
@@ -0,0 +1,14 @@
1
+ declare class RedisCache {
2
+ private client;
3
+ private locker;
4
+ private lockTimeout;
5
+ private lockDuration;
6
+ private locks;
7
+ private baseTTL;
8
+ private lockRetries;
9
+ constructor(options: any);
10
+ get(key: any): Promise<any>;
11
+ set(key: any, value: any): Promise<void>;
12
+ private lock;
13
+ }
14
+ export default RedisCache;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const redis_1 = __importDefault(require("redis"));
16
+ const redis_lock_1 = __importDefault(require("redis-lock"));
17
+ const bluebird_1 = __importDefault(require("bluebird"));
18
+ const util_1 = require("util");
19
+ const errors_1 = require("./errors");
20
+ bluebird_1.default.promisifyAll(redis_1.default.RedisClient.prototype);
21
+ bluebird_1.default.promisifyAll(redis_1.default.Multi.prototype);
22
+ const { env } = process;
23
+ const HOST = env.REDIS_HOST_NAME || '127.0.0.1';
24
+ const PORT = env.REDIS_HOST_PORT || 6379;
25
+ const DEFAULT_LOCK_TIMEOUT = 5000;
26
+ const DEFAULT_LOCK_DURATION = 1000;
27
+ const DEFAULT_BASE_TTL = 3600;
28
+ class RedisCache {
29
+ constructor(options) {
30
+ var _a, _b, _c, _d;
31
+ this.client = redis_1.default.createClient({
32
+ host: options.host || HOST,
33
+ port: options.port || PORT,
34
+ });
35
+ this.locker = util_1.promisify(redis_lock_1.default(this.client, (_a = options.lockRetries) !== null && _a !== void 0 ? _a : 10));
36
+ this.lockTimeout = (_b = options.lockTimeout) !== null && _b !== void 0 ? _b : DEFAULT_LOCK_TIMEOUT;
37
+ this.lockDuration = (_c = options.lockDuration) !== null && _c !== void 0 ? _c : DEFAULT_LOCK_DURATION;
38
+ this.baseTTL = (_d = options.ttl) !== null && _d !== void 0 ? _d : DEFAULT_BASE_TTL;
39
+ this.locks = {};
40
+ }
41
+ get(key) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ let lock;
44
+ try {
45
+ // Try to lock the key.
46
+ lock = yield this.lock(key);
47
+ }
48
+ catch (err) {
49
+ throw new errors_1.RedisLockError('Failed to lock key', err, key);
50
+ }
51
+ // If the lock did not fail, add it to a locks dictionary.
52
+ this.locks[key] = lock;
53
+ let value;
54
+ try {
55
+ // Try to get the value from redis.
56
+ value = yield this.client.getAsync(key);
57
+ }
58
+ catch (err) {
59
+ throw new errors_1.RedisCacheError('Failed to get a value', err);
60
+ }
61
+ return JSON.parse(value);
62
+ });
63
+ }
64
+ set(key, value) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const ttl = parseInt(String(this.baseTTL * (Math.random() + 1)), 10);
67
+ try {
68
+ yield this.client.setAsync(key, JSON.stringify(value), 'EX', ttl);
69
+ }
70
+ catch (err) {
71
+ throw new errors_1.RedisCacheError('Failed to set a key-value pair', err);
72
+ }
73
+ });
74
+ }
75
+ lock(key) {
76
+ return Promise.race([
77
+ this.locker(key, this.lockDuration),
78
+ new Promise((resolve, reject) => setTimeout(() => reject(), this.lockTimeout)),
79
+ ]);
80
+ }
81
+ }
82
+ exports.default = RedisCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/matmon",
3
- "version": "1.0.0",
3
+ "version": "1.0.2-beta-1",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -29,8 +29,11 @@
29
29
  "dependencies": {
30
30
  "@types/node": "^14.14.20",
31
31
  "async-mutex": "^0.2.6",
32
+ "bluebird": "^3.7.2",
32
33
  "dotenv": "^8.2.0",
33
- "lru-cache": "^6.0.0"
34
+ "lru-cache": "^6.0.0",
35
+ "redis": "^3.0.2",
36
+ "redis-lock": "^0.1.4"
34
37
  },
35
38
  "devDependencies": {
36
39
  "typescript": "^3.9.5"