diff --git a/src/lib/server/db/mongo.ts b/src/lib/server/db/mongo.ts index 48f7b6430..90ebfbad8 100644 --- a/src/lib/server/db/mongo.ts +++ b/src/lib/server/db/mongo.ts @@ -1,7 +1,7 @@ import { MONGO_DATABASE, MONGO_HOST, MONGO_PORT } from "$env/static/private"; +import { MongoClient } from "mongodb"; import { updateCollections } from "./mongo/update-collections"; import { updateItems } from "./mongo/update-items"; -import { MongoClient } from "mongodb"; const client = new MongoClient(`mongodb://${MONGO_HOST}:${MONGO_PORT}/${MONGO_DATABASE}`); diff --git a/src/lib/server/db/redis.ts b/src/lib/server/db/redis.ts index e0c578f65..968124fb2 100644 --- a/src/lib/server/db/redis.ts +++ b/src/lib/server/db/redis.ts @@ -1,5 +1,5 @@ +import { REDIS_HOST, REDIS_PASSWORD, REDIS_PORT } from "$env/static/private"; import { createClient } from "redis"; -import { REDIS_PASSWORD, REDIS_HOST, REDIS_PORT } from "$env/static/private"; export const REDIS = createClient({ url: `redis://default:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}` diff --git a/src/lib/server/lib.ts b/src/lib/server/lib.ts index 6441e1342..c4ea28d36 100644 --- a/src/lib/server/lib.ts +++ b/src/lib/server/lib.ts @@ -1,9 +1,9 @@ -import type { ProfilesResponse, Profile } from "$types/global"; import { HYPIXEL_API_KEY } from "$env/static/private"; -import { SkyCryptError } from "../constants/error"; +import { isPlayer } from "$params/player"; import { isUUID } from "$params/uuid"; +import type { Profile, ProfilesResponse } from "$types/global"; +import { SkyCryptError } from "../constants/error"; import { REDIS } from "./db/redis"; -import { isPlayer } from "$params/player"; const headers = { Accept: "application/json", "User-Agent": "SkyCrypt", "API-KEY": HYPIXEL_API_KEY }; diff --git a/src/lib/server/lib/stats.ts b/src/lib/server/lib/stats.ts index 6e81f76a4..2bf6ad7d3 100644 --- a/src/lib/server/lib/stats.ts +++ b/src/lib/server/lib/stats.ts @@ -1,14 +1,19 @@ +import { REDIS } from "$lib/server/db/redis"; import { getProfiles } from "$lib/server/lib"; import * as stats from "$lib/server/stats/stats"; import type { Profile, Stats } from "$types/global"; import type { Player } from "$types/raw/player/lib"; -const getAccessories = stats.getAccessories; -const getPets = stats.getPets; -const getMainStats = stats.getMainStats; -const getCollections = stats.getCollections; +const { getAccessories, getPets, getMainStats, getCollections } = stats; export async function getStats(profile: Profile, player: Player): Promise { + const timeNow = Date.now(); + const cache = await REDIS.get(`STATS:${profile.uuid}`); + if (cache) { + console.log(`[CACHE] Found cache for ${profile.uuid} in ${Date.now() - timeNow}ms`); + return JSON.parse(cache); + } + const userProfile = profile.members[profile.uuid]; const items = await stats.getItems(userProfile); @@ -29,7 +34,7 @@ export async function getStats(profile: Profile, player: Player): Promise getCollections(userProfile, profile) ]); - return { + const output = { username: player.displayname, uuid: profile.uuid, profile_id: profile.profile_id, @@ -59,4 +64,8 @@ export async function getStats(profile: Profile, player: Player): Promise rift: stats.getRift(userProfile), misc: stats.getMisc(userProfile, profile, player) }; + + await REDIS.SETEX(`STATS:${profile.uuid}`, 60 * 5, JSON.stringify(output)); + + return output; }