UNPKG

8.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var PlatformTools_1 = require("../platform/PlatformTools");
5/**
6 * Caches query result into Redis database.
7 */
8var RedisQueryResultCache = /** @class */ (function () {
9 // -------------------------------------------------------------------------
10 // Constructor
11 // -------------------------------------------------------------------------
12 function RedisQueryResultCache(connection, clientType) {
13 this.connection = connection;
14 this.clientType = clientType;
15 this.redis = this.loadRedis();
16 }
17 // -------------------------------------------------------------------------
18 // Public Methods
19 // -------------------------------------------------------------------------
20 /**
21 * Creates a connection with given cache provider.
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 /*return*/];
56 });
57 });
58 };
59 /**
60 * Disconnects the connection
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 /*return*/, 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 * Creates table for storing cache if it does not exist yet.
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 /*return*/];
84 });
85 });
86 };
87 /**
88 * Caches given query result.
89 * Returns cache result if found.
90 * Returns undefined if result is not cached.
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 * Checks if cache is expired or not.
116 */
117 RedisQueryResultCache.prototype.isExpired = function (savedCache) {
118 return (savedCache.time + savedCache.duration) < new Date().getTime();
119 };
120 /**
121 * Stores given query result in the cache.
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 /*return*/, 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 * Clears everything stored in the cache.
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 /*return*/, 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 * Removes all cached results by given identifiers from cache.
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 /*yield*/, Promise.all(identifiers.map(function (identifier) {
172 return _this.deleteKey(identifier);
173 }))];
174 case 1:
175 _a.sent();
176 return [2 /*return*/];
177 }
178 });
179 });
180 };
181 // -------------------------------------------------------------------------
182 // Protected Methods
183 // -------------------------------------------------------------------------
184 /**
185 * Removes a single key from redis database.
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 * Loads redis dependency.
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}());
210exports.RedisQueryResultCache = RedisQueryResultCache;
211
212//# sourceMappingURL=RedisQueryResultCache.js.map