diff --git a/app/src/api.js b/app/src/api.js index 4e17228..0f18e48 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1,5 +1,6 @@ import $ from 'jquery'; import * as Cookies from "js-cookie"; +import Cache from "./cache"; const URL = "https://hack.battlecode.org"; // const URL = 'http://localhost:8000'; // DEVELOPMENT @@ -84,17 +85,26 @@ class Api { } static getUserTeam(callback) { - $.get(URL+"/api/userteam/"+encodeURIComponent(Cookies.get('username'))+"/"+LEAGUE+"/").done(function(data, status){ - Cookies.set('team_id',data.id); - Cookies.set('team_name',data.name); - - $.get(URL+"/api/"+LEAGUE+"/team/"+data.id+"/").done(function(data, status) { - callback(data); - }); - }).fail(function(xhr, status, error) { - // possibly dangerous??? - callback(null); - }); + + var requestURL = URL+"/api/userteam/"+encodeURIComponent(Cookies.get('username'))+"/"+LEAGUE+"/"; + var data = Cache.getCache(requestURL); + + if (data !==null) { + callback(data); + } else { + $.get(requestURL).done(function(data, status){ + Cookies.set('team_id',data.id); + Cookies.set('team_name',data.name); + + $.get(URL+"/api/"+LEAGUE+"/team/"+data.id+"/").done(function(data, status) { + Cache.setCache(requestURL, data); + callback(data); + }); + }).fail(function(xhr, status, error) { + // possibly dangerous??? + callback(null); + }); + } } static updateTeam(params, callback) { @@ -256,17 +266,41 @@ class Api { } static getUserProfile(callback) { - $.get(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/").done(function(data, status) { - Cookies.set('user_url',data.url); - $.get(data.url).done(function(data, success) { - callback(data); - }).fail(function(xhr, status, error) { - console.log(error); + var profileURL = URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"; + var data = Cache.getCache(profileURL); + + if (data!==null) { + callback(data); + var userURLData = Cache.getCache(data.url); + if (userURLData!==null) { + callback(userURLData); + } else { + $.get(data.url).done(function(userURLData, success) { + Cache.setCache(data.url, userURLData); + callback(userURLData); + }).fail(function(xhr, status, error) { + console.log(error); + }); + } + } else { + $.get(profileURL).done(function(data, status) { + Cache.setCache(profileURL, data); + Cookies.set('user_url', data.url); + + $.get(data.url).done(function(userURLData, success) { + Cache.setCache(data.url, userURLData); + callback(userURLData); + }).fail(function(xhr, status, error) { + console.log(error); + }); }); - }); + } } static updateUser(profile, callback) { + Cache.deleteCache(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"); + Cache.deleteCache(Cookies.get('user_url')); + $.ajax({ url:Cookies.get('user_url'), data:JSON.stringify(profile), @@ -297,6 +331,7 @@ class Api { static logout(callback) { Cookies.set('token',""); Cookies.set('refresh',""); + Cache.clearCache(); callback(); } diff --git a/app/src/cache.js b/app/src/cache.js new file mode 100644 index 0000000..4380da7 --- /dev/null +++ b/app/src/cache.js @@ -0,0 +1,36 @@ + +export default class Cache { + static setCache(url, data, timeout = 1800) { + data['expiry_time'] = (new Date()).getTime() + timeout * 1000; + window.localStorage.setItem(url, JSON.stringify(data)); + console.log("Store:"); + console.log(data); + } + + static getCache(url) { + var cachedResult = window.localStorage.getItem(url); + console.log("getCache from url:"); + console.log(url); + console.log(cachedResult); + console.log((new Date()).getTime()); + if (cachedResult===null) { + return null; + } else { + var parsedResult = JSON.parse(cachedResult); + if ((new Date()).getTime() < parsedResult['expiry_time']) { + console.log("Cache Hit"); + return parsedResult; + } else { + return null; + } + } + } + + static deleteCache(url) { + window.localStorage.removeItem(url); + } + + static clearCache() { + window.localStorage.clear(); + } +} \ No newline at end of file