1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var PlatformTools_1 = require("../platform/PlatformTools");
|
5 |
|
6 |
|
7 |
|
8 | var RedisQueryResultCache = (function () {
|
9 |
|
10 |
|
11 |
|
12 | function RedisQueryResultCache(connection, clientType) {
|
13 | this.connection = connection;
|
14 | this.clientType = clientType;
|
15 | this.redis = this.loadRedis();
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 | |
21 |
|
22 |
|
23 | RedisQueryResultCache.prototype.connect = function () {
|
24 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
25 | var cacheOptions;
|
26 | return tslib_1.__generator(this, function (_a) {
|
27 | cacheOptions = this.connection.options.cache;
|
28 | if (this.clientType === "redis") {
|
29 | if (cacheOptions && cacheOptions.options) {
|
30 | this.client = this.redis.createClient(cacheOptions.options);
|
31 | }
|
32 | else {
|
33 | this.client = this.redis.createClient();
|
34 | }
|
35 | }
|
36 | else if (this.clientType === "ioredis") {
|
37 | if (cacheOptions && cacheOptions.options) {
|
38 | this.client = new this.redis(cacheOptions.options);
|
39 | }
|
40 | else {
|
41 | this.client = new this.redis();
|
42 | }
|
43 | }
|
44 | else if (this.clientType === "ioredis/cluster") {
|
45 | if (cacheOptions && cacheOptions.options && cacheOptions.options instanceof Array) {
|
46 | this.client = new this.redis.Cluster(cacheOptions.options);
|
47 | }
|
48 | else if (cacheOptions && cacheOptions.options && cacheOptions.options.startupNodes) {
|
49 | this.client = new this.redis.Cluster(cacheOptions.options.startupNodes, cacheOptions.options.options);
|
50 | }
|
51 | else {
|
52 | throw new Error("options.startupNodes required for " + this.clientType + ".");
|
53 | }
|
54 | }
|
55 | return [2 ];
|
56 | });
|
57 | });
|
58 | };
|
59 | |
60 |
|
61 |
|
62 | RedisQueryResultCache.prototype.disconnect = function () {
|
63 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
64 | var _this = this;
|
65 | return tslib_1.__generator(this, function (_a) {
|
66 | return [2 , new Promise(function (ok, fail) {
|
67 | _this.client.quit(function (err, result) {
|
68 | if (err)
|
69 | return fail(err);
|
70 | ok();
|
71 | _this.client = undefined;
|
72 | });
|
73 | })];
|
74 | });
|
75 | });
|
76 | };
|
77 | |
78 |
|
79 |
|
80 | RedisQueryResultCache.prototype.synchronize = function (queryRunner) {
|
81 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
82 | return tslib_1.__generator(this, function (_a) {
|
83 | return [2 ];
|
84 | });
|
85 | });
|
86 | };
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 | RedisQueryResultCache.prototype.getFromCache = function (options, queryRunner) {
|
93 | var _this = this;
|
94 | return new Promise(function (ok, fail) {
|
95 | if (options.identifier) {
|
96 | _this.client.get(options.identifier, function (err, result) {
|
97 | if (err)
|
98 | return fail(err);
|
99 | ok(JSON.parse(result));
|
100 | });
|
101 | }
|
102 | else if (options.query) {
|
103 | _this.client.get(options.query, function (err, result) {
|
104 | if (err)
|
105 | return fail(err);
|
106 | ok(JSON.parse(result));
|
107 | });
|
108 | }
|
109 | else {
|
110 | ok(undefined);
|
111 | }
|
112 | });
|
113 | };
|
114 | |
115 |
|
116 |
|
117 | RedisQueryResultCache.prototype.isExpired = function (savedCache) {
|
118 | return (savedCache.time + savedCache.duration) < new Date().getTime();
|
119 | };
|
120 | |
121 |
|
122 |
|
123 | RedisQueryResultCache.prototype.storeInCache = function (options, savedCache, queryRunner) {
|
124 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
125 | var _this = this;
|
126 | return tslib_1.__generator(this, function (_a) {
|
127 | return [2 , new Promise(function (ok, fail) {
|
128 | if (options.identifier) {
|
129 | _this.client.set(options.identifier, JSON.stringify(options), "PX", options.duration, function (err, result) {
|
130 | if (err)
|
131 | return fail(err);
|
132 | ok();
|
133 | });
|
134 | }
|
135 | else if (options.query) {
|
136 | _this.client.set(options.query, JSON.stringify(options), "PX", options.duration, function (err, result) {
|
137 | if (err)
|
138 | return fail(err);
|
139 | ok();
|
140 | });
|
141 | }
|
142 | })];
|
143 | });
|
144 | });
|
145 | };
|
146 | |
147 |
|
148 |
|
149 | RedisQueryResultCache.prototype.clear = function (queryRunner) {
|
150 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
151 | var _this = this;
|
152 | return tslib_1.__generator(this, function (_a) {
|
153 | return [2 , new Promise(function (ok, fail) {
|
154 | _this.client.flushdb(function (err, result) {
|
155 | if (err)
|
156 | return fail(err);
|
157 | ok();
|
158 | });
|
159 | })];
|
160 | });
|
161 | });
|
162 | };
|
163 | |
164 |
|
165 |
|
166 | RedisQueryResultCache.prototype.remove = function (identifiers, queryRunner) {
|
167 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
168 | var _this = this;
|
169 | return tslib_1.__generator(this, function (_a) {
|
170 | switch (_a.label) {
|
171 | case 0: return [4 , Promise.all(identifiers.map(function (identifier) {
|
172 | return _this.deleteKey(identifier);
|
173 | }))];
|
174 | case 1:
|
175 | _a.sent();
|
176 | return [2 ];
|
177 | }
|
178 | });
|
179 | });
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 | |
185 |
|
186 |
|
187 | RedisQueryResultCache.prototype.deleteKey = function (key) {
|
188 | var _this = this;
|
189 | return new Promise(function (ok, fail) {
|
190 | _this.client.del(key, function (err, result) {
|
191 | if (err)
|
192 | return fail(err);
|
193 | ok();
|
194 | });
|
195 | });
|
196 | };
|
197 | |
198 |
|
199 |
|
200 | RedisQueryResultCache.prototype.loadRedis = function () {
|
201 | try {
|
202 | return PlatformTools_1.PlatformTools.load(this.clientType);
|
203 | }
|
204 | catch (e) {
|
205 | throw new Error("Cannot use cache because " + this.clientType + " is not installed. Please run \"npm i " + this.clientType + " --save\".");
|
206 | }
|
207 | };
|
208 | return RedisQueryResultCache;
|
209 | }());
|
210 | exports.RedisQueryResultCache = RedisQueryResultCache;
|
211 |
|
212 |
|