mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
refactor: add redis cache in Riot API wrapper (Jax)
This commit is contained in:
parent
fb57006b0d
commit
ae6b67edcf
21 changed files with 74 additions and 77 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
const { Command } = require('@adonisjs/ace')
|
const { Command } = require('@adonisjs/ace')
|
||||||
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
||||||
const Database = use('Database')
|
const Database = use('Database')
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const DetailedMatch = use('App/Models/DetailedMatch')
|
const DetailedMatch = use('App/Models/DetailedMatch')
|
||||||
const Queue = use('Bee/Queue')
|
const Queue = use('Bee/Queue')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
const { Command } = require('@adonisjs/ace')
|
const { Command } = require('@adonisjs/ace')
|
||||||
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
|
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
|
||||||
const Database = use('Database')
|
const Database = use('Database')
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const Match = use('App/Models/Match')
|
const Match = use('App/Models/Match')
|
||||||
const Queue = use('Bee/Queue')
|
const Queue = use('Bee/Queue')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const DetailedMatch = use('App/Models/DetailedMatch')
|
const DetailedMatch = use('App/Models/DetailedMatch')
|
||||||
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
||||||
const MatchService = use('App/Services/MatchService')
|
const MatchService = use('App/Services/MatchService')
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const LiveMatchTransformer = use('App/Transformers/LiveMatchTransformer')
|
const LiveMatchTransformer = use('App/Transformers/LiveMatchTransformer')
|
||||||
const MatchRepository = make('App/Repositories/MatchRepository')
|
const MatchRepository = make('App/Repositories/MatchRepository')
|
||||||
const MatchService = use('App/Services/MatchService')
|
const MatchService = use('App/Services/MatchService')
|
||||||
|
|
|
||||||
4
server/app/Services/Jax/index.js
Normal file
4
server/app/Services/Jax/index.js
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
const Jax = require('./src/Jax')
|
||||||
|
const Config = require('./JaxConfig')
|
||||||
|
|
||||||
|
module.exports = new Jax(Config)
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
const got = require('got')
|
const got = require('got')
|
||||||
|
const Redis = use('Redis')
|
||||||
|
|
||||||
class CDragonRequest {
|
class CDragonRequest {
|
||||||
constructor(endpoint) {
|
constructor(endpoint, cacheTime) {
|
||||||
this.endpoint = endpoint
|
this.endpoint = endpoint
|
||||||
|
this.cacheTime = cacheTime
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json
|
// 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
|
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json
|
||||||
|
|
||||||
async execute() {
|
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 {
|
try {
|
||||||
const response = await got(url);
|
const response = await got(url);
|
||||||
|
|
||||||
|
await Redis.set(url, response.body, 'EX', this.cacheTime)
|
||||||
return JSON.parse(response.body)
|
return JSON.parse(response.body)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error.response.body);
|
console.log(error.response.body);
|
||||||
25
server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js
Normal file
25
server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js
Normal 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
|
||||||
|
|
@ -11,7 +11,8 @@ class LeagueEndpoint {
|
||||||
region,
|
region,
|
||||||
this.config,
|
this.config,
|
||||||
`league/v4/entries/by-summoner/${summonerID}`,
|
`league/v4/entries/by-summoner/${summonerID}`,
|
||||||
this.limiter
|
this.limiter,
|
||||||
|
300
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -13,7 +13,8 @@ class MatchEndpoint {
|
||||||
region,
|
region,
|
||||||
this.config,
|
this.config,
|
||||||
`match/v4/matches/${matchID}`,
|
`match/v4/matches/${matchID}`,
|
||||||
this.limiter
|
this.limiter,
|
||||||
|
1500
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -11,7 +11,8 @@ class MatchlistEndpoint {
|
||||||
region,
|
region,
|
||||||
this.config,
|
this.config,
|
||||||
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
|
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
|
||||||
this.limiter
|
this.limiter,
|
||||||
|
1500
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -11,7 +11,8 @@ class SpectatorEndpoint {
|
||||||
region,
|
region,
|
||||||
this.config,
|
this.config,
|
||||||
`spectator/v4/active-games/by-summoner/${summonerID}`,
|
`spectator/v4/active-games/by-summoner/${summonerID}`,
|
||||||
this.limiter
|
this.limiter,
|
||||||
|
0
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -11,7 +11,8 @@ class SummonerEndpoint {
|
||||||
region,
|
region,
|
||||||
this.config,
|
this.config,
|
||||||
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
|
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
|
||||||
this.limiter
|
this.limiter,
|
||||||
|
36000
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,26 +1,40 @@
|
||||||
const { promisify } = require('util')
|
const { promisify } = require('util')
|
||||||
|
const Redis = use('Redis')
|
||||||
|
|
||||||
class JaxRequest {
|
class JaxRequest {
|
||||||
constructor(region, config, endpoint, limiter) {
|
constructor(region, config, endpoint, limiter, cacheTime) {
|
||||||
this.region = region
|
this.region = region
|
||||||
this.config = config
|
this.config = config
|
||||||
this.endpoint = endpoint
|
this.endpoint = endpoint
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
|
this.cacheTime = cacheTime
|
||||||
this.retries = config.requestOptions.retriesBeforeAbort
|
this.retries = config.requestOptions.retriesBeforeAbort
|
||||||
|
|
||||||
this.sleep = promisify(setTimeout)
|
this.sleep = promisify(setTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute() {
|
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 {
|
try {
|
||||||
const resp = await this.limiter.executing({
|
const resp = await this.limiter.executing({
|
||||||
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
|
url,
|
||||||
token: this.config.key,
|
token: this.config.key,
|
||||||
resolveWithFullResponse: false
|
resolveWithFullResponse: false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (this.cacheTime > 0) {
|
||||||
|
await Redis.set(url, resp, 'EX', this.cacheTime)
|
||||||
|
}
|
||||||
return JSON.parse(resp)
|
return JSON.parse(resp)
|
||||||
|
|
||||||
} catch ({ statusCode, ...rest }) {
|
} catch ({ statusCode, ...rest }) {
|
||||||
this.retries--
|
this.retries--
|
||||||
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Logger = use('Logger')
|
const Logger = use('Logger')
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
|
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
|
||||||
const { getSeasonNumber } = use('App/helpers')
|
const { getSeasonNumber } = use('App/helpers')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const Redis = use('Redis')
|
|
||||||
|
|
||||||
class SummonerService {
|
class SummonerService {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
@ -28,16 +27,7 @@ class SummonerService {
|
||||||
*/
|
*/
|
||||||
async getAccount(summonerName, region) {
|
async getAccount(summonerName, region) {
|
||||||
const name = summonerName.toLowerCase().replace(/ /g, '')
|
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)
|
const account = await Jax.Summoner.summonerName(name, region)
|
||||||
if (account) {
|
|
||||||
await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000)
|
|
||||||
}
|
|
||||||
return account
|
return account
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,19 +37,12 @@ class SummonerService {
|
||||||
* @param region
|
* @param region
|
||||||
*/
|
*/
|
||||||
async getRanked(account, 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 ranked = await Jax.League.summonerID(account.id, region)
|
||||||
const result = {
|
const result = {
|
||||||
soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null,
|
soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null,
|
||||||
flex5v5: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_SR')) || 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
|
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
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Jax = use('Jax')
|
const Jax = use('App/Services/Jax')
|
||||||
const Helpers = use('App/helpers')
|
const Helpers = use('App/helpers')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -19,7 +19,6 @@ const providers = [
|
||||||
'lucid-mongo/providers/LucidMongoProvider',
|
'lucid-mongo/providers/LucidMongoProvider',
|
||||||
'@adonisjs/redis/providers/RedisProvider',
|
'@adonisjs/redis/providers/RedisProvider',
|
||||||
|
|
||||||
join(__dirname, '../providers/Jax/JaxProvider'),
|
|
||||||
join(__dirname, '../providers/Queue/Provider')
|
join(__dirname, '../providers/Queue/Provider')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue