Skip to content
Antoine CLOP edited this page Aug 1, 2020 · 3 revisions

Cache

To increase performances, LeagueAPI uses cache system.

What's a cache system?

Cache is in-memory storage which can contain any kind of data. Those data are then read by methods to respond user's calls. By doing this, LeagueAPI framework can return a response very quickly when data was cached previously.

To understand how it works let's make an example: If a friend needs help for his homework and asks you to solve: (x+4) / 2 * 367 + 3982 = 43, you'll make the calculs and then give the result to your friend. But you're smart and think that another friend might ask you that same question later for that same homework. So you write down the solution on a paper (x = -25.47). And then yeah, you was right and your other friend asks for help too. You just have to quickly read your paper and give him the solution.

Riot API methods are not cached since those data can receive modifications at any time. So LeagueAPI needs to make request to Riot API everytime to make sure datas are up to date. But because champion's skins, statistics or lore does not receive updates during a patch, those methods are cached. Thus the first request will have a normal delay but next calls will be significantly faster.

Cache is saved in memory, which means if application is closed, everything will be lost. But if the application is not closed during multiple days, data may become outdated. So LeagueAPI let you explicitly clear cache, which will force framework to make request to Riot's servers to get fresh datas.

Take advantage of cache

The more you understand and adapt your code to how LeagueAPI cache works, the more your code will execute fastly! Network requests are the most time consuming tasks. Since cache only uses network for unowned data, the first request will be the longest. Knowing that, you'll understand why example n°2 is quicker.

'Slow' version

league.lolAPI.getChampionDetails(byName: "Ahri") { (ahriDetails, errorMsg) in
    if let ahriDetails = ahriDetails {
        print("Success!")
    }
    else {
        print("Request failed cause: \(errorMsg ?? "No error description")")
    }
}
// We're making a second call for Xerath details but since it is asynchrone,
// Ahri et Xerath requests will start together, not using cache performance help.
league.lolAPI.getChampionDetails(byName: "Xerath") { (xerathDetails, errorMsg) in
    if let xerathDetails = xerathDetails {
        print("Success!")
    }
    else {
        print("Request failed cause: \(errorMsg ?? "No error description")")
    }
}

Executing with logs:

Requesting: https://ddragon.leagueoflegends.com/realms/na.json  
Requesting: https://ddragon.leagueoflegends.com/realms/na.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion/Ahri.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion/Xerath.json  
Success!  
Success!  

'Fast' version

league.lolAPI.getChampionDetails(byName: "Ahri") { (ahriDetails, errorMsg) in
    if let ahriDetails = ahriDetails {
        print("Success!")
        // Since we're making 2 requests on Champion data, cache will already have some informations
        // and will only make necessary calls for Xerath details
        self.league.lolAPI.getChampionDetails(byName: "Xerath") { (xerathDetails, errorMsg) in
            if let xerathDetails = xerathDetails {
                print("Success!")
            }
            else {
                print("Request failed cause: \(errorMsg ?? "No error description")")
            }
        }
    }
    else {
        print("Request failed cause: \(errorMsg ?? "No error description")")
    }
}

Executing with logs:

Requesting: https://ddragon.leagueoflegends.com/realms/na.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion.json  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion/Ahri.json  
Success!  
Requesting: https://ddragon.leagueoflegends.com/cdn/8.16.1/data/en_US/champion/Xerath.json  
Success!

We see that in the first case, requests are doubled. It may look like it was faster but in reality no. Slow version had both results in 216ms whereas fast version got it in 188ms on an average connection speed. It could have been way slower with a bad connection. And it is also a good practice to save user's cellular data.

Methods

  • clearCache()

ClearCache

Parameters

This procedure clears internal cache but does not return anything since it always succeed.

Usage example

league.clearCache()
Clone this wiki locally