Skip to content

Commit

Permalink
perf(stats): cache stats
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckySoLucky committed Nov 24, 2024
1 parent f0a7526 commit 7efefa9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/server/db/mongo.ts
Original file line number Diff line number Diff line change
@@ -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}`);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/db/redis.ts
Original file line number Diff line number Diff line change
@@ -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}`
Expand Down
6 changes: 3 additions & 3 deletions src/lib/server/lib.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down
19 changes: 14 additions & 5 deletions src/lib/server/lib/stats.ts
Original file line number Diff line number Diff line change
@@ -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<Stats> {
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);
Expand All @@ -29,7 +34,7 @@ export async function getStats(profile: Profile, player: Player): Promise<Stats>
getCollections(userProfile, profile)
]);

return {
const output = {
username: player.displayname,
uuid: profile.uuid,
profile_id: profile.profile_id,
Expand Down Expand Up @@ -59,4 +64,8 @@ export async function getStats(profile: Profile, player: Player): Promise<Stats>
rift: stats.getRift(userProfile),
misc: stats.getMisc(userProfile, profile, player)
};

await REDIS.SETEX(`STATS:${profile.uuid}`, 60 * 5, JSON.stringify(output));

return output;
}

0 comments on commit 7efefa9

Please sign in to comment.