refactor: add redis cache in Riot API wrapper (Jax)

This commit is contained in:
Valentin Kaelin 2020-04-26 20:07:17 +02:00
parent fb57006b0d
commit ae6b67edcf
21 changed files with 74 additions and 77 deletions

View file

@ -3,7 +3,7 @@
const { Command } = require('@adonisjs/ace')
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
const Database = use('Database')
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const DetailedMatch = use('App/Models/DetailedMatch')
const Queue = use('Bee/Queue')

View file

@ -3,7 +3,7 @@
const { Command } = require('@adonisjs/ace')
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
const Database = use('Database')
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const Match = use('App/Models/Match')
const Queue = use('Bee/Queue')

View file

@ -1,6 +1,6 @@
'use strict'
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const DetailedMatch = use('App/Models/DetailedMatch')
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
const MatchService = use('App/Services/MatchService')

View file

@ -1,6 +1,6 @@
'use strict'
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const LiveMatchTransformer = use('App/Transformers/LiveMatchTransformer')
const MatchRepository = make('App/Repositories/MatchRepository')
const MatchService = use('App/Services/MatchService')

View file

@ -0,0 +1,4 @@
const Jax = require('./src/Jax')
const Config = require('./JaxConfig')
module.exports = new Jax(Config)

View file

@ -1,8 +1,10 @@
const got = require('got')
const Redis = use('Redis')
class CDragonRequest {
constructor(endpoint) {
constructor(endpoint, cacheTime) {
this.endpoint = endpoint
this.cacheTime = cacheTime
}
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json
@ -11,10 +13,17 @@ class CDragonRequest {
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json
async execute() {
let url = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/${this.endpoint}`
const url = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/${this.endpoint}`
const requestCached = await Redis.get(url)
if (requestCached) {
return JSON.parse(requestCached)
}
try {
const response = await got(url);
await Redis.set(url, response.body, 'EX', this.cacheTime)
return JSON.parse(response.body)
} catch (error) {
console.log(error.response.body);

View file

@ -0,0 +1,25 @@
const CDragonRequest = require('../CDragonRequest')
class CDragonEndpoint {
champions() {
return new CDragonRequest('champion-summary.json', 36000).execute()
}
items() {
return new CDragonRequest('items.json', 36000).execute()
}
perks() {
return new CDragonRequest('perks.json', 36000).execute()
}
perkstyles() {
return new CDragonRequest('perkstyles.json', 36000).execute()
}
summonerSpells() {
return new CDragonRequest('summoner-spells.json', 36000).execute()
}
}
module.exports = CDragonEndpoint

View file

@ -11,7 +11,8 @@ class LeagueEndpoint {
region,
this.config,
`league/v4/entries/by-summoner/${summonerID}`,
this.limiter
this.limiter,
300
).execute()
}
}

View file

@ -13,7 +13,8 @@ class MatchEndpoint {
region,
this.config,
`match/v4/matches/${matchID}`,
this.limiter
this.limiter,
1500
).execute()
}
}

View file

@ -11,7 +11,8 @@ class MatchlistEndpoint {
region,
this.config,
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
this.limiter
this.limiter,
1500
).execute()
}
}

View file

@ -11,7 +11,8 @@ class SpectatorEndpoint {
region,
this.config,
`spectator/v4/active-games/by-summoner/${summonerID}`,
this.limiter
this.limiter,
0
).execute()
}
}

View file

@ -11,7 +11,8 @@ class SummonerEndpoint {
region,
this.config,
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
this.limiter
this.limiter,
36000
).execute()
}
}

View file

@ -1,26 +1,40 @@
const { promisify } = require('util')
const Redis = use('Redis')
class JaxRequest {
constructor(region, config, endpoint, limiter) {
constructor(region, config, endpoint, limiter, cacheTime) {
this.region = region
this.config = config
this.endpoint = endpoint
this.limiter = limiter
this.cacheTime = cacheTime
this.retries = config.requestOptions.retriesBeforeAbort
this.sleep = promisify(setTimeout)
}
async execute() {
const url = `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`
// Redis cache
if (this.cacheTime > 0) {
const requestCached = await Redis.get(url)
if (requestCached) {
return JSON.parse(requestCached)
}
}
try {
const resp = await this.limiter.executing({
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
url,
token: this.config.key,
resolveWithFullResponse: false
})
if (this.cacheTime > 0) {
await Redis.set(url, resp, 'EX', this.cacheTime)
}
return JSON.parse(resp)
} catch ({ statusCode, ...rest }) {
this.retries--

View file

@ -1,7 +1,7 @@
'use strict'
const Logger = use('Logger')
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
const { getSeasonNumber } = use('App/helpers')

View file

@ -1,7 +1,6 @@
'use strict'
const Jax = use('Jax')
const Redis = use('Redis')
const Jax = use('App/Services/Jax')
class SummonerService {
constructor() {
@ -28,16 +27,7 @@ class SummonerService {
*/
async getAccount(summonerName, region) {
const name = summonerName.toLowerCase().replace(/ /g, '')
const accountCache = await Redis.get(`${region}-${name}`)
if (accountCache) {
console.log('ACCOUNT CACHED')
return JSON.parse(accountCache)
}
const account = await Jax.Summoner.summonerName(name, region)
if (account) {
await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000)
}
return account
}
@ -47,19 +37,12 @@ class SummonerService {
* @param region
*/
async getRanked(account, region) {
const rankedCache = await Redis.get(`ranked-${account.puuid}`)
if (rankedCache) {
console.log('RANKED CACHED')
return JSON.parse(rankedCache)
}
const ranked = await Jax.League.summonerID(account.id, region)
const result = {
soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null,
flex5v5: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_SR')) || null,
flex3v3: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_TT')) || null
}
await Redis.set(`ranked-${account.puuid}`, JSON.stringify(result), 'EX', 1500)
return result
}
}

View file

@ -1,6 +1,6 @@
'use strict'
const Jax = use('Jax')
const Jax = use('App/Services/Jax')
const Helpers = use('App/helpers')
/**

View file

@ -1,17 +0,0 @@
const { ServiceProvider } = require('@adonisjs/fold')
const Jax = require('./src/Jax')
const Config = require('./JaxConfig')
class JaxProvider extends ServiceProvider {
register () {
this.app.singleton('Jax', () => {
return new Jax(Config)
})
}
boot() {
use('Jax')
}
}
module.exports = JaxProvider

View file

@ -1,25 +0,0 @@
const CDragonRequest = require('../CDragonRequest')
class CDragonEndpoint {
champions() {
return new CDragonRequest('champion-summary.json').execute()
}
items() {
return new CDragonRequest('items.json').execute()
}
perks() {
return new CDragonRequest('perks.json').execute()
}
perkstyles() {
return new CDragonRequest('perkstyles.json').execute()
}
summonerSpells() {
return new CDragonRequest('summoner-spells.json').execute()
}
}
module.exports = CDragonEndpoint

View file

@ -19,7 +19,6 @@ const providers = [
'lucid-mongo/providers/LucidMongoProvider',
'@adonisjs/redis/providers/RedisProvider',
join(__dirname, '../providers/Jax/JaxProvider'),
join(__dirname, '../providers/Queue/Provider')
]