Skip to content

Commit aa17555

Browse files
committed
refactor getInfo
1 parent 021f24b commit aa17555

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

redisinsight/api/src/modules/cluster-monitor/cluster-monitor.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class ClusterMonitorService {
4141
return Promise.reject(new BadRequestException('Current database is not in a cluster mode'));
4242
}
4343

44-
const info = await client.getInfo(true, 'server');
44+
const info = await client.getInfo('server');
4545

4646
const strategy = this.getClusterInfoStrategy(get(info, 'server.redis_version'));
4747

redisinsight/api/src/modules/recommendation/providers/recommendation.provider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class RecommendationProvider {
3939
redisClient: RedisClient,
4040
): Promise<Recommendation> {
4141
try {
42-
const info = await redisClient.getInfo(true, 'memory');
42+
const info = await redisClient.getInfo('memory');
4343
const nodesNumbersOfCachedScripts = get(info, 'memory.number_of_cached_scripts');
4444

4545
return parseInt(nodesNumbersOfCachedScripts, 10) > LUA_SCRIPT_RECOMMENDATION_COUNT
@@ -90,7 +90,7 @@ export class RecommendationProvider {
9090
return null;
9191
}
9292
try {
93-
const info = await redisClient.getInfo(true, 'keyspace');
93+
const info = await redisClient.getInfo('keyspace');
9494
const keyspace = get(info, 'keyspace', {});
9595
const databasesWithKeys = Object.values(keyspace).filter((db) => {
9696
const { keys } = convertMultilineReplyToObject(db as string, ',', '=');
@@ -283,7 +283,7 @@ export class RecommendationProvider {
283283
redisClient: RedisClient,
284284
): Promise<Recommendation> {
285285
try {
286-
const info = await redisClient.getInfo(true, 'clients');
286+
const info = await redisClient.getInfo('clients');
287287
const connectedClients = parseInt(get(info, 'clients.connected_clients'), 10);
288288

289289
return connectedClients > BIG_AMOUNT_OF_CONNECTED_CLIENTS_RECOMMENDATION_CLIENTS
@@ -325,7 +325,7 @@ export class RecommendationProvider {
325325
redisClient: RedisClient,
326326
): Promise<Recommendation> {
327327
try {
328-
const info = await redisClient.getInfo(true, 'server');
328+
const info = await redisClient.getInfo('server');
329329
const version = get(info, 'server.redis_version');
330330
return semverCompare(version, REDIS_VERSION_RECOMMENDATION_VERSION) >= 0
331331
? null

redisinsight/api/src/modules/redis/client/redis.client.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export enum RedisFeature {
5151
export abstract class RedisClient extends EventEmitter2 {
5252
public readonly id: string;
5353

54-
protected info: any;
54+
protected _redisVersion: string | undefined;
5555

5656
protected lastTimeUsed: number;
5757

@@ -147,8 +147,8 @@ export abstract class RedisClient extends EventEmitter2 {
147147
switch (feature) {
148148
case RedisFeature.HashFieldsExpiration:
149149
try {
150-
const info = await this.getInfo(false);
151-
return info?.['server']?.['redis_version'] && semverCompare('7.3', info['server']['redis_version']) < 1;
150+
const redisVersion = await this.getRedisVersion();
151+
return redisVersion && semverCompare('7.3', redisVersion) < 1;
152152
} catch (e) {
153153
return false;
154154
}
@@ -157,39 +157,39 @@ export abstract class RedisClient extends EventEmitter2 {
157157
}
158158
}
159159

160+
private async getRedisVersion(): Promise<string> {
161+
if (!this._redisVersion) {
162+
const infoData = await this.getInfo('server');
163+
this._redisVersion = infoData?.server?.redis_version;
164+
}
165+
166+
return this._redisVersion;
167+
}
168+
160169
/**
161170
* Get redis database info
162171
* Uses cache by default
163172
* @param force
164173
* @param infoSection - e.g. server, clients, memory, etc.
165174
*/
166-
public async getInfo(force = true, infoSection?: string) {
167-
if (force || !this.info) {
168-
try {
169-
const infoData = convertRedisInfoReplyToObject(await this.call(
170-
infoSection ? ['info', infoSection] : ['info'],
171-
{ replyEncoding: 'utf8' },
172-
) as string);
173-
174-
this.info = {
175-
...this.info,
176-
...infoData,
177-
}
178-
} catch (error) {
179-
if (error.message.includes(ERROR_MESSAGES.NO_INFO_COMMAND_PERMISSION)) {
180-
try {
181-
// Fallback to getting basic information from `hello` command
182-
this.info = await this.getRedisHelloInfo();
183-
} catch (_error) {
184-
this.info = UNKNOWN_REDIS_INFO;
185-
}
186-
} else {
187-
this.info = UNKNOWN_REDIS_INFO;
175+
public async getInfo(infoSection?: string) {
176+
try {
177+
return convertRedisInfoReplyToObject(await this.call(
178+
infoSection ? ['info', infoSection] : ['info'],
179+
{ replyEncoding: 'utf8' },
180+
) as string);
181+
} catch (error) {
182+
if (error.message.includes(ERROR_MESSAGES.NO_INFO_COMMAND_PERMISSION)) {
183+
try {
184+
// Fallback to getting basic information from `hello` command
185+
return await this.getRedisHelloInfo();
186+
} catch (_error) {
187+
// Ignore: hello is not available pre redis version 6
188188
}
189189
}
190190
}
191191

192-
return this.info;
192+
return UNKNOWN_REDIS_INFO;
193193
}
194194

195195
private async getRedisHelloInfo() {

redisinsight/api/src/modules/redis/utils/keys.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { convertMultilineReplyToObject } from 'src/modules/redis/utils/reply.uti
55
export const getTotalKeysFromInfo = async (client: RedisClient) => {
66
try {
77
const currentDbIndex = await client.getCurrentDbIndex();
8-
const info = await client.getInfo(true, 'keyspace');
8+
const info = await client.getInfo('keyspace');
99

1010
const dbInfo = get(info, 'keyspace', {});
1111
if (!dbInfo[`db${currentDbIndex}`]) {

0 commit comments

Comments
 (0)