Skip to content

Commit 1eb7fd9

Browse files
committed
[ADD] Cache for lametric
1 parent d4b6041 commit 1eb7fd9

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

src/fivem/fivem.controller.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ServerTrackedDto from 'src/dto/serverTrackedDto';
77
import ServerCfxDto from 'src/dto/serverCfxDto';
88
import { fivemCfxResponse, fivemPlayersResponse, fivemResponse } from './fivem.schema';
99
import { CACHE_MANAGER } from '@nestjs/cache-manager';
10+
import { CacheKeys } from 'src/utils/enums';
1011

1112
/*
1213
CODE CacheKey
@@ -33,15 +34,15 @@ export class FivemController {
3334
schema: fivemResponse
3435
})
3536
async trackServer(@Param() address: ServerTrackedDto): Promise<any> {
36-
const cache: any = await this.cacheManager.get(`FM:${address.address}`);
37+
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveM}:${address.address}`);
3738
let result: any;
3839

3940
if (cache)
4041
return cache;
4142
result = await this.service.trackServer(address);
4243
result["cacheTime"] = Math.floor(Date.now() / 1000);
4344
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
44-
this.cacheManager.set(`FM:${address.address}`, result, 5 * 60 * 1000);
45+
this.cacheManager.set(`${CacheKeys.FiveM}:${address.address}`, result, 5 * 60 * 1000);
4546
return result;
4647
}
4748

@@ -55,15 +56,15 @@ export class FivemController {
5556
schema: fivemCfxResponse
5657
})
5758
async trackServerByCfx(@Param() code: ServerCfxDto): Promise<any> {
58-
const cache: any = await this.cacheManager.get(`FMCFX:${code.code}`);
59+
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveMCfxCode}:${code.code}`);
5960
let result: any;
6061

6162
if (cache)
6263
return cache;
6364
result = await this.service.trackServerByCfx(code);
6465
result["cacheTime"] = Math.floor(Date.now() / 1000);
6566
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
66-
this.cacheManager.set(`FMCFX:${code.code}`, result, 5 * 60 * 1000);
67+
this.cacheManager.set(`${CacheKeys.FiveMCfxCode}:${code.code}`, result, 5 * 60 * 1000);
6768
return result;
6869
}
6970

@@ -77,15 +78,15 @@ export class FivemController {
7778
schema: fivemPlayersResponse
7879
})
7980
async trackPlayers(@Param() address: ServerTrackedDto): Promise<any> {
80-
const cache: any = await this.cacheManager.get(`FMP:${address.address}`);
81+
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveMPlayers}:${address.address}`);
8182
let result: any;
8283

8384
if (cache)
8485
return cache;
8586
result = await this.service.trackPlayers(address);
8687
result["cacheTime"] = Math.floor(Date.now() / 1000);
8788
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
88-
this.cacheManager.set(`FMP:${address.address}`, result, 5 * 60 * 1000);
89+
this.cacheManager.set(`${CacheKeys.FiveMPlayers}:${address.address}`, result, 5 * 60 * 1000);
8990
return result;
9091
}
9192
}

src/lametric/lametric.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { MinecraftModule } from 'src/minecraft/minecraft.module';
44
import { SourceModule } from 'src/source/source.module';
55
import { FivemModule } from 'src/fivem/fivem.module';
66
import { LametricService } from './lametric.service';
7+
import { CacheModule } from '@nestjs/cache-manager';
78

89
@Module({
9-
imports: [MinecraftModule, SourceModule, FivemModule],
10+
imports: [CacheModule.register(), MinecraftModule, SourceModule, FivemModule],
1011
controllers: [LametricController], providers: [LametricService]
1112
})
1213
export class LametricModule {}

src/lametric/lametric.service.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Inject, Injectable } from '@nestjs/common';
22
import { FivemService } from 'src/fivem/fivem.service';
33
import { MinecraftService } from 'src/minecraft/minecraft.service';
44
import { SourceService } from 'src/source/source.service';
55
import LametricFrameDto from './dto/lametricFrameDto';
66
import LametricServerCheckedDto from './dto/lametricServerCheckedDto';
7-
import { LametricIconServer, LametricServerTypeParams } from 'src/utils/enums';
7+
import {
8+
CacheKeys,
9+
LametricIconServer,
10+
LametricServerTypeParams,
11+
} from 'src/utils/enums';
812
import LametricFrameTextDto from './dto/lametricFrameTextDto';
13+
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
914

1015
@Injectable()
1116
export class LametricService {
1217
constructor(
18+
@Inject(CACHE_MANAGER) private cacheManager: Cache,
1319
private readonly minecraftService: MinecraftService,
1420
private readonly sourceService: SourceService,
1521
private readonly fivemService: FivemService,
@@ -30,6 +36,16 @@ export class LametricService {
3036
this.fivemService.trackServerByCfx({ code: code }),
3137
};
3238

39+
readonly cacheDict: {
40+
[id in LametricServerTypeParams]: string;
41+
} = {
42+
Minecraft: CacheKeys.Minecraft,
43+
MinecraftBedrock: CacheKeys.MinecraftBedrock,
44+
Source: CacheKeys.Source,
45+
FiveM: CacheKeys.FiveM,
46+
FiveMCfxCode: CacheKeys.FiveMCfxCode,
47+
};
48+
3349
readonly serverIconDict: {
3450
[serverType in LametricServerTypeParams]: LametricIconServer;
3551
} = {
@@ -44,13 +60,24 @@ export class LametricService {
4460
serverChecked: LametricServerCheckedDto,
4561
): Promise<LametricFrameDto> {
4662
const icon: LametricIconServer = this.serverIconDict[serverChecked.type];
63+
const cache: any = await this.cacheManager.get(
64+
`${this.cacheDict[serverChecked.type]}:${serverChecked.address}`,
65+
);
4766
const frame: LametricFrameDto = {
4867
frames: [new LametricFrameTextDto(serverChecked.name, icon)],
4968
};
69+
let result: any;
5070

51-
let result = await this.actionDict[serverChecked.type](
52-
serverChecked.address,
53-
);
71+
if (cache) {
72+
result = cache;
73+
} else {
74+
result = await this.actionDict[serverChecked.type](serverChecked.address);
75+
this.cacheManager.set(
76+
`${this.cacheDict[serverChecked.type]}:${serverChecked.address}`,
77+
result,
78+
5 * 60 * 1000,
79+
);
80+
}
5481
if (!result.online) {
5582
frame.frames.push(new LametricFrameTextDto('OFFLINE', icon));
5683
return frame;

src/minecraft/minecraft.controller.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ServerTrackedDto from 'src/dto/serverTrackedDto';
66
import { Cache } from 'cache-manager';
77
import { bedrockResponse, javaQueryResponse, javaResponse } from './minecraft.schema';
88
import { CACHE_MANAGER } from '@nestjs/cache-manager';
9+
import { CacheKeys } from 'src/utils/enums';
910

1011
/*
1112
CODE CacheKey
@@ -40,7 +41,7 @@ export class MinecraftController {
4041
result = await this.service.trackServer(address);
4142
result["cacheTime"] = Math.floor(Date.now() / 1000);
4243
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
43-
this.cacheManager.set(`MC:${address.address}`, result, 5 * 60 * 1000);
44+
this.cacheManager.set(`${CacheKeys.Minecraft}:${address.address}`, result, 5 * 60 * 1000);
4445
return result;
4546
}
4647

@@ -54,7 +55,7 @@ export class MinecraftController {
5455
schema: javaQueryResponse
5556
})
5657
async trackServerQuery(@Param() address: ServerTrackedDto): Promise<any> {
57-
const cache: any = await this.cacheManager.get(`MCQ:${address.address}`);
58+
const cache: any = await this.cacheManager.get(`${CacheKeys.MinecraftQuery}:${address.address}`);
5859
let result: any;
5960

6061
if (cache)
@@ -76,7 +77,7 @@ export class MinecraftController {
7677
schema: bedrockResponse
7778
})
7879
async trackBedrockServer(@Param() address: ServerTrackedDto): Promise<any> {
79-
const cache: any = await this.cacheManager.get(`MCB:${address.address}`);
80+
const cache: any = await this.cacheManager.get(`${CacheKeys.MinecraftBedrock}:${address.address}`);
8081
let result: any;
8182

8283
if (cache)

src/source/source.controller.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ServerTrackedDto from 'src/dto/serverTrackedDto';
55
import { Cache } from 'cache-manager';
66
import { sourcePlayersResponse, sourceResponse, sourceRulesResponse } from './source.schema';
77
import { CACHE_MANAGER } from '@nestjs/cache-manager';
8+
import { CacheKeys } from 'src/utils/enums';
89

910
/*
1011
CODE CacheKey
@@ -31,15 +32,15 @@ export class SourceController {
3132
schema: sourceResponse
3233
})
3334
async trackServer(@Param() address: ServerTrackedDto): Promise<any> {
34-
const cache: any = await this.cacheManager.get(`SO:${address.address}`);
35+
const cache: any = await this.cacheManager.get(`${CacheKeys.Source}:${address.address}`);
3536
let result: any;
3637

3738
if (cache)
3839
return cache;
3940
result = await this.service.trackServer(address);
4041
result["cacheTime"] = Math.floor(Date.now() / 1000);
4142
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
42-
this.cacheManager.set(`SO:${address.address}`, result, 5 * 60 * 1000);
43+
this.cacheManager.set(`${CacheKeys.Source}:${address.address}`, result, 5 * 60 * 1000);
4344
return result;
4445
}
4546

@@ -53,15 +54,15 @@ export class SourceController {
5354
schema: sourcePlayersResponse
5455
})
5556
async trackPlayers(@Param() address: ServerTrackedDto): Promise<any> {
56-
const cache: any = await this.cacheManager.get(`SOP:${address.address}`);
57+
const cache: any = await this.cacheManager.get(`${CacheKeys.SourcePlayers}:${address.address}`);
5758
let result: any;
5859

5960
if (cache)
6061
return cache;
6162
result = await this.service.trackPlayers(address);
6263
result["cacheTime"] = Math.floor(Date.now() / 1000);
6364
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
64-
this.cacheManager.set(`SOP:${address.address}`, result, 5 * 60 * 1000);
65+
this.cacheManager.set(`${CacheKeys.SourcePlayers}:${address.address}`, result, 5 * 60 * 1000);
6566
return result;
6667
}
6768

@@ -75,15 +76,15 @@ export class SourceController {
7576
schema: sourceRulesResponse
7677
})
7778
async trackRules(@Param() address: ServerTrackedDto): Promise<any> {
78-
const cache: any = await this.cacheManager.get(`SOR:${address.address}`);
79+
const cache: any = await this.cacheManager.get(`${CacheKeys.SourceRules}:${address.address}`);
7980
let result: any;
8081

8182
if (cache)
8283
return cache;
8384
result = await this.service.trackRules(address);
8485
result["cacheTime"] = Math.floor(Date.now() / 1000);
8586
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
86-
this.cacheManager.set(`SOR:${address.address}`, result, 5 * 60 * 1000);
87+
this.cacheManager.set(`${CacheKeys.SourceRules}:${address.address}`, result, 5 * 60 * 1000);
8788
return result;
8889
}
8990
}

src/utils/enums.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@ enum LametricServerTypeParams {
2020
MinecraftBedrock = "MinecraftBedrock"
2121
};
2222

23+
enum CacheKeys {
24+
FiveM = "FM",
25+
FiveMCfxCode = "FMCFX",
26+
FiveMPlayers = "FMP",
27+
Minecraft = "MC",
28+
MinecraftQuery = "MCQ",
29+
MinecraftBedrock = "MCB",
30+
Source = "SO",
31+
SourcePlayers = "SOP",
32+
SourceRules = "SOR"
33+
}
34+
2335
export {
2436
DefaultPort,
2537
LametricIconServer,
26-
LametricServerTypeParams
38+
LametricServerTypeParams,
39+
CacheKeys
2740
}

0 commit comments

Comments
 (0)