mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2025-03-19 06:30:13 +03:00
Merge pull request #61 from tigersoldier/fix-game-data-cache
Use per-server cache
This commit is contained in:
commit
2b90b4e5db
2 changed files with 21 additions and 32 deletions
|
@ -65,38 +65,26 @@ export default class Game {
|
|||
|
||||
/**
|
||||
* Get latest game data, including voice data and packages difference
|
||||
*
|
||||
*
|
||||
* @returns JSON from API else throws Error if company's servers are unreachable or if they responded with an error
|
||||
*/
|
||||
public static getLatestData(): Promise<Data>
|
||||
{
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const response = await fetch(constants.versionsUri(await this.server));
|
||||
|
||||
if (response.ok)
|
||||
{
|
||||
const cache = await Cache.get('Game.getLatestData.ServerResponse');
|
||||
|
||||
if (cache && !cache.expired)
|
||||
resolve(cache.value as Data);
|
||||
|
||||
else
|
||||
{
|
||||
const json: ServerResponse = JSON.parse(await response.body());
|
||||
|
||||
if (json.message == 'OK')
|
||||
{
|
||||
Cache.set('Game.getLatestData.ServerResponse', json.data, 6 * 3600);
|
||||
|
||||
resolve(json.data);
|
||||
}
|
||||
|
||||
else reject(new Error(`${constants.placeholders.uppercase.company}'s versions server responds with an error: [${json.retcode}] ${json.message}`));
|
||||
}
|
||||
public static async getLatestData(): Promise<Data> {
|
||||
const cache = await Cache.get(`Game.getLatestData.ServerResponse.${await this.server}`);
|
||||
if (cache && !cache.expired) {
|
||||
return cache.value as Data;
|
||||
}
|
||||
const response = await fetch(constants.versionsUri(await this.server));
|
||||
if (response.ok) {
|
||||
const json: ServerResponse = JSON.parse(await response.body());
|
||||
if (json.message == 'OK') {
|
||||
Cache.set(`Game.getLatestData.ServerResponse.${await this.server}`, json.data, 6 * 3600);
|
||||
return json.data;
|
||||
} else {
|
||||
throw new Error(`${constants.placeholders.uppercase.company}'s versions server responds with an error: [${json.retcode}] ${json.message}`);
|
||||
}
|
||||
|
||||
else reject(new Error(`${constants.placeholders.uppercase.company}'s versions server is unreachable`));
|
||||
});
|
||||
} else {
|
||||
throw new Error(`${constants.placeholders.uppercase.company}'s versions server is unreachable`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -277,8 +277,9 @@ export default class Patch
|
|||
public static getPatchInfo(version: string, source: 'origin' | 'additional' = 'origin'): Promise<PatchInfo|null>
|
||||
{
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const patchCacheKey = `Patch.getPatchInfo.${version}.${source}.${await Game.server}`;
|
||||
const resolveOutput = async (output: PatchInfo|null, unityPlayerHash: string|null = null) => {
|
||||
await Cache.set(`Patch.getPatchInfo.${version}.${source}`, {
|
||||
await Cache.set(patchCacheKey, {
|
||||
available: true,
|
||||
output: output,
|
||||
playerHash: unityPlayerHash
|
||||
|
@ -290,7 +291,7 @@ export default class Patch
|
|||
const rejectOutput = async (error: Error) => {
|
||||
// Cache this error only on an hour
|
||||
// because then the server can become alive
|
||||
await Cache.set(`Patch.getPatchInfo.${version}.${source}`, {
|
||||
await Cache.set(patchCacheKey, {
|
||||
available: false,
|
||||
error: error.message
|
||||
}, 3600);
|
||||
|
@ -298,7 +299,7 @@ export default class Patch
|
|||
reject(error);
|
||||
};
|
||||
|
||||
const cache = await Cache.get(`Patch.getPatchInfo.${version}.${source}`);
|
||||
const cache = await Cache.get(patchCacheKey);
|
||||
|
||||
// If we have result cached
|
||||
if (cache && !cache.expired)
|
||||
|
|
Loading…
Add table
Reference in a new issue