Skip to content

Commit b65fb97

Browse files
committed
FLS-1452 - Simplify code in AdapterCacheService around Redis client
Currently we have duplicate Redis client configuration code in the private AdapterCacheService method getRedisClient and in the catboxProvider function. The way the code is formatted is also unnecessarily space-consuming. This commit creates a new shared utility function createRedisClient which can be used in both places, and uses concise formatting to reduce lines of code significantly.
1 parent f71a52e commit b65fb97

File tree

1 file changed

+32
-82
lines changed

1 file changed

+32
-82
lines changed

runner/src/server/services/AdapterCacheService.ts

Lines changed: 32 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ enum ADDITIONAL_IDENTIFIER {
4242
Confirmation = ":confirmation",
4343
}
4444

45+
const createRedisClient = (): Redis | null => {
46+
if (redisHost || redisUri) {
47+
const redisOptions: {password?: string; tls?: {};} = {};
48+
if (redisPassword) redisOptions.password = redisPassword;
49+
if (redisTls) redisOptions.tls = {};
50+
51+
return isSingleRedis
52+
? new Redis(redisUri ?? {host: redisHost, port: redisPort, password: redisPassword})
53+
: new Redis.Cluster(
54+
[{host: redisHost, port: redisPort}],
55+
{dnsLookup: (address, callback) => callback(null, address, 4), redisOptions}
56+
);
57+
}
58+
return null;
59+
};
60+
4561
export class AdapterCacheService extends CacheService {
4662

4763
constructor(server: HapiServer) {
@@ -253,49 +269,16 @@ export class AdapterCacheService extends CacheService {
253269
throw Boom.notFound("Cannot find the given form");
254270
}
255271

256-
private getRedisClient() {
257-
if (redisHost || redisUri) {
258-
const redisOptions: {
259-
password?: string;
260-
tls?: {};
261-
} = {};
262-
263-
if (redisPassword) {
264-
redisOptions.password = redisPassword;
265-
}
266-
267-
if (redisTls) {
268-
redisOptions.tls = {};
269-
}
270-
271-
const client = isSingleRedis
272-
? new Redis(
273-
redisUri ?? {
274-
host: redisHost,
275-
port: redisPort,
276-
password: redisPassword,
277-
}
278-
)
279-
: new Redis.Cluster(
280-
[
281-
{
282-
host: redisHost,
283-
port: redisPort,
284-
},
285-
],
286-
{
287-
dnsLookup: (address, callback) => callback(null, address, 4),
288-
redisOptions,
289-
}
290-
);
291-
return client;
292-
} else {
293-
console.log({
294-
...LOGGER_DATA,
295-
message: `[FORM-CACHE] using memory caching`,
296-
})
297-
}
298-
}
272+
private getRedisClient(): Redis | null {
273+
const client = createRedisClient();
274+
if (!client) {
275+
console.log({
276+
...LOGGER_DATA,
277+
message: `[FORM-CACHE] using memory caching`,
278+
});
279+
}
280+
return client;
281+
}
299282
}
300283

301284
export const catboxProvider = () => {
@@ -305,49 +288,16 @@ export const catboxProvider = () => {
305288
*/
306289
const provider = {
307290
constructor: redisHost || redisUri ? CatboxRedis.Engine : CatboxMemory.Engine,
308-
options: {},
291+
options: {partition},
309292
};
310293

311294
if (redisHost || redisUri) {
312-
console.log("Starting redis session management")
313-
const redisOptions: {
314-
password?: string;
315-
tls?: {};
316-
} = {};
317-
318-
if (redisPassword) {
319-
redisOptions.password = redisPassword;
320-
}
321-
322-
if (redisTls) {
323-
redisOptions.tls = {};
324-
}
325-
326-
const client = isSingleRedis
327-
? new Redis(
328-
redisUri ?? {
329-
host: redisHost,
330-
port: redisPort,
331-
password: redisPassword,
332-
}
333-
)
334-
: new Redis.Cluster(
335-
[
336-
{
337-
host: redisHost,
338-
port: redisPort,
339-
},
340-
],
341-
{
342-
dnsLookup: (address, callback) => callback(null, address, 4),
343-
redisOptions,
344-
}
345-
);
346-
provider.options = {client, partition};
347-
console.log(`Redis Url : ${redisUri} session management`);
295+
console.log("Starting redis session management");
296+
const client = createRedisClient();
297+
provider.options = {client, partition};
298+
console.log(`Redis Url : ${redisUri} session management`);
348299
} else {
349-
console.log("Starting in memory session management")
350-
provider.options = {partition};
300+
console.log("Starting in memory session management");
351301
}
352302

353303
return provider;

0 commit comments

Comments
 (0)