1 | ;
|
2 | /**
|
3 | * This file is part of the @egodigital/egoose distribution.
|
4 | * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
|
5 | *
|
6 | * @egodigital/egoose is free software: you can redistribute it and/or modify
|
7 | * it under the terms of the GNU Lesser General Public License as
|
8 | * published by the Free Software Foundation, version 3.
|
9 | *
|
10 | * @egodigital/egoose is distributed in the hope that it will be useful, but
|
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 | * Lesser General Public License for more details.
|
14 | *
|
15 | * You should have received a copy of the GNU Lesser General Public License
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | */
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const _ = require("lodash");
|
20 | const redis = require("redis");
|
21 | const index_1 = require("../index");
|
22 | const index_2 = require("./index");
|
23 | /**
|
24 | * A Redis cache (client).
|
25 | */
|
26 | class RedisCache extends index_2.CacheBase {
|
27 | /**
|
28 | * Initializes a new instance of that class.
|
29 | *
|
30 | * @param {RedisCacheOptions} options The options.
|
31 | */
|
32 | constructor(options) {
|
33 | super();
|
34 | this.options = options;
|
35 | }
|
36 | createClient() {
|
37 | let host = index_1.toStringSafe(this.options.host)
|
38 | .trim();
|
39 | if ('' === host) {
|
40 | host = '127.0.0.1';
|
41 | }
|
42 | let port = parseInt(index_1.toStringSafe(this.options.port)
|
43 | .trim());
|
44 | if (isNaN(port)) {
|
45 | port = 6379;
|
46 | }
|
47 | return redis.createClient({
|
48 | host: host,
|
49 | port: port,
|
50 | });
|
51 | }
|
52 | /**
|
53 | * Creates a new instance from environment variable.
|
54 | *
|
55 | * @return {RedisCache} The new instance.
|
56 | */
|
57 | static fromEnvironment() {
|
58 | return new RedisCache({
|
59 | host: process.env.REDIS_HOST,
|
60 | port: parseInt(index_1.toStringSafe(process.env.REDIS_HOST_PORT)
|
61 | .trim())
|
62 | });
|
63 | }
|
64 | /** @inheritdoc */
|
65 | async getInner(key, defaultValue) {
|
66 | return await this.withConnection((client) => {
|
67 | return new Promise((resolve, reject) => {
|
68 | try {
|
69 | client.get(key, (err, value) => {
|
70 | try {
|
71 | if (err) {
|
72 | reject(err);
|
73 | }
|
74 | else {
|
75 | if (_.isNil(value)) {
|
76 | resolve(defaultValue);
|
77 | }
|
78 | else {
|
79 | resolve(JSON.parse(value));
|
80 | }
|
81 | }
|
82 | }
|
83 | catch (e) {
|
84 | reject(e);
|
85 | }
|
86 | });
|
87 | }
|
88 | catch (e) {
|
89 | reject(e);
|
90 | }
|
91 | });
|
92 | });
|
93 | }
|
94 | /** @inheritdoc */
|
95 | async setInner(key, value, opts) {
|
96 | let ttl = parseInt(index_1.toStringSafe(this.getOptionValue(opts, 'ttl'))
|
97 | .trim());
|
98 | if (isNaN(ttl)) {
|
99 | ttl = false;
|
100 | }
|
101 | await this.withConnection((client) => {
|
102 | return new Promise((resolve, reject) => {
|
103 | try {
|
104 | if (_.isNil(value)) {
|
105 | // no data => delete
|
106 | client.del(key, (err) => {
|
107 | if (err) {
|
108 | reject(err);
|
109 | }
|
110 | else {
|
111 | resolve();
|
112 | }
|
113 | });
|
114 | }
|
115 | else {
|
116 | const VALUE_TO_SAVE = JSON.stringify(value);
|
117 | const CALLBACK = (err) => {
|
118 | if (err) {
|
119 | reject(err);
|
120 | }
|
121 | else {
|
122 | resolve();
|
123 | }
|
124 | };
|
125 | if (false === ttl) {
|
126 | client.set(key, VALUE_TO_SAVE, CALLBACK);
|
127 | }
|
128 | else {
|
129 | client.set(key, VALUE_TO_SAVE, 'EX', ttl, CALLBACK);
|
130 | }
|
131 | }
|
132 | }
|
133 | catch (e) {
|
134 | reject(e);
|
135 | }
|
136 | });
|
137 | });
|
138 | }
|
139 | async withConnection(func) {
|
140 | const CLIENT = this.createClient();
|
141 | try {
|
142 | return await Promise.resolve(func(CLIENT));
|
143 | }
|
144 | finally {
|
145 | CLIENT.quit();
|
146 | }
|
147 | }
|
148 | }
|
149 | exports.RedisCache = RedisCache;
|
150 | //# sourceMappingURL=redis.js.map |
\ | No newline at end of file |