Skip to content

Commit

Permalink
feat: cache history (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDams authored Mar 10, 2023
1 parent fdd1943 commit 77107f7
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 267 deletions.
18 changes: 18 additions & 0 deletions functions/dateStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const CustomType = require('../templates/customType')
const CustomTypeFunc = require('../functions/customType')
const { getPagination } = require('./pagination')
const { getInteractionOption } = require('./commands')
const { caching } = require('cache-manager')

const ttl = 60 * 1000 * 5 // 5 minutes
const cachingMemory = caching('memory', {
max: 100,
ttl: ttl,
})

const generatePlayerStats = playerHistory => {
const playerStats = {
Expand Down Expand Up @@ -73,10 +80,16 @@ const getAverage = (q, d, fixe = 2, percent = 1) => ((parseFloat(q) / parseFloat

const getPlayerHistory = async (playerId, maxMatch, eloMatches = true) => {
const playerStats = await Player.getStats(playerId)
const cacheHistory = await cachingMemory
const limit = 100
let playerHistory = []
const cacheName = `${playerId}-${eloMatches ? 'elo' : 'match'}`

if (maxMatch === null || maxMatch > playerStats.lifetime.Matches) maxMatch = playerStats.lifetime.Matches

const cache = await cacheHistory.get(cacheName)
if (cache && cache.length == playerStats.lifetime.Matches) return cache.slice(0, maxMatch)

if (eloMatches) {
for (let page = 0; page < Math.ceil(maxMatch / limit); page++) playerHistory.push(...await Match.getMatchElo(playerId, maxMatch, page))

Expand All @@ -97,6 +110,11 @@ const getPlayerHistory = async (playerId, maxMatch, eloMatches = true) => {
playerHistory.push(...(await Player.getHistory(playerId, limit, page * limit)).items)
}

if (maxMatch == playerStats.lifetime.Matches) {
if (cache) await cacheHistory.del(cacheName)
await cacheHistory.set(cacheName, playerHistory, ttl)
}

return playerHistory
}

Expand Down
Loading

0 comments on commit 77107f7

Please sign in to comment.