Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions apps/api/src/guild/guild.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ import { Guild, GuildMember, Player, deserialize, serialize } from "@statsify/sc
import { GuildLeaderboardService } from "./leaderboards/guild-leaderboard.service.js";
import { HypixelService } from "#hypixel";
import { InjectModel } from "@m8a/nestjs-typegoose";
import { InjectRedis } from "#redis";
import { Injectable, ServiceUnavailableException } from "@nestjs/common";
import { Logger } from "@statsify/logger";
import { PlayerService } from "#player";
import { Redis } from "ioredis";
import { flatten } from "@statsify/util";
import type { ReturnModelType } from "@typegoose/typegoose";

const GUILD_CACHE_TIME_MS = 600_000;

@Injectable()
export class GuildService {
private readonly logger = new Logger("GuildService");
Expand All @@ -31,7 +35,8 @@ export class GuildService {
private readonly playerService: PlayerService,
private readonly guildLeaderboardService: GuildLeaderboardService,
@InjectModel(Guild) private readonly guildModel: ReturnModelType<typeof Guild>,
@InjectModel(Player) private readonly playerModel: ReturnModelType<typeof Player>
@InjectModel(Player) private readonly playerModel: ReturnModelType<typeof Player>,
@InjectRedis() private readonly redis: Redis
) {}

public async get(
Expand All @@ -52,6 +57,15 @@ export class GuildService {
};
}

if (
type === GuildQuery.PLAYER &&
!cachedGuild &&
cache !== CacheLevel.LIVE &&
await this.redis.exists(`guildless:${tag}`)
) {
throw new GuildNotFoundException(displayName);
}

let guild: Guild | null;

try {
Expand All @@ -71,6 +85,7 @@ export class GuildService {
}

if (!guild) {
if (type === GuildQuery.PLAYER) await this.redis.set(`guildless:${tag}`, 1, "PX", GUILD_CACHE_TIME_MS);
await this.handleGuildNotFound(cachedGuild, tag, type);
throw new GuildNotFoundException(displayName);
}
Expand Down Expand Up @@ -152,7 +167,7 @@ export class GuildService {
}

// Cache guilds responses for 10 minutes
guild.expiresAt = Date.now() + 600_000;
guild.expiresAt = Date.now() + GUILD_CACHE_TIME_MS;

const flatGuild = flatten(guild);
const serializedGuild = serialize(Guild, flatGuild);
Expand Down
Loading