From 9166d11009d8fc45d41d21568290d2096449a7b7 Mon Sep 17 00:00:00 2001 From: AzureFractal Date: Tue, 25 Sep 2018 21:07:15 -0400 Subject: [PATCH 1/3] Add caching utilities to api.js. Add use of caching for getUserProfile and updateUser. --- app/src/api.js | 67 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/app/src/api.js b/app/src/api.js index 6192cc3..3a728c3 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -5,6 +5,36 @@ var URL = "https://hack.battlecode.org"; var LEAGUE = 0 class Api { + static setCache(url, data) { + data['expiry_time'] = (new Date()).getTime() + 300000; + window.sessionStorage.setItem(url, JSON.stringify(data)); + console.log("Store:"); + console.log(data); + } + + static getCache(url) { + var cachedResult = window.sessionStorage.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.sessionStorage.removeItem(url); + } + static getUpcomingDates(callback) { var new_state = [ {id: 0, date: 'hi', data: 'message'}, @@ -213,17 +243,40 @@ 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 = Api.getCache(profileURL); + if (data!==null) { + callback(data); + var userURLData = Api.getCache(data.url); + if (userURLData!==null) { + callback(userURLData); + } else { + $.get(data.url).done(function(userURLData, success) { + Api.setCache(data.url, userURLData); + callback(userURLData); + }).fail(function(xhr, status, error) { + console.log(error); + }); + } + } else { + $.get(profileURL).done(function(data, status) { + Api.setCache(profileURL, data); + Cookies.set('user_url', data.url); + + $.get(data.url).done(function(userURLData, success) { + Api.setCache(data.url, userURLData); + callback(userURLData); + }).fail(function(xhr, status, error) { + console.log(error); + }); }); - }); + } } static updateUser(profile, callback) { + Api.deleteCache(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"); + Api.deleteCache(Cookies.get('user_url')); + $.ajax({ url:Cookies.get('user_url'), data:JSON.stringify(profile), From ef35e29def1dad9d251d00db04c692583f6c4a89 Mon Sep 17 00:00:00 2001 From: AzureFractal Date: Sat, 20 Oct 2018 13:29:53 -0400 Subject: [PATCH 2/3] Move caching functions to separate .js file. --- app/src/api.js | 87 +++++++++++++++++++++++------------------------- app/src/cache.js | 36 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 app/src/cache.js diff --git a/app/src/api.js b/app/src/api.js index 3a728c3..3c25845 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1,40 +1,11 @@ import $ from 'jquery'; import * as Cookies from "js-cookie"; +import Cache from "./cache"; var URL = "https://hack.battlecode.org"; -var LEAGUE = 0 +var LEAGUE = 0; class Api { - static setCache(url, data) { - data['expiry_time'] = (new Date()).getTime() + 300000; - window.sessionStorage.setItem(url, JSON.stringify(data)); - console.log("Store:"); - console.log(data); - } - - static getCache(url) { - var cachedResult = window.sessionStorage.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.sessionStorage.removeItem(url); - } - static getUpcomingDates(callback) { var new_state = [ {id: 0, date: 'hi', data: 'message'}, @@ -78,18 +49,40 @@ class Api { } static getUserTeam(callback) { - $.get(URL+"/api/"+LEAGUE+"/team/?username="+encodeURIComponent(Cookies.get('username'))).done(function(data, status){ + var requestURL = URL+"/api/"+LEAGUE+"/team/?username="+encodeURIComponent(Cookies.get('username')); + var data = Cache.getCache(requestURL); + + if (data!==null) { if (data.results.length === 0) callback(null); else { - Cookies.set('team_id',data.results[0].id); - Cookies.set('team_name',data.results[0].name); - $.get(URL+"/api/"+LEAGUE+"/team/"+data.results[0].id+"/").done(function(data, status) { + var secRequestURL = URL+"/api/"+LEAGUE+"/team/"+data.results[0].id+"/"; + var secData = Cache.getCache(secRequestURL); + if (secData!==null) { callback(data); - }); + } else { + $.get(secRequestURL).done(function(data, status) { + callback(data); + Cache.setCache(secRequestURL, data); + }); + } } - }).fail(function(xhr, status, error) { - callback(false); - }); + } else { + $.get(requestURL).done(function(data, status){ + if (data.results.length === 0) callback(null); + else { + Cookies.set('team_id',data.results[0].id); + Cookies.set('team_name',data.results[0].name); + var secRequestURL = URL+"/api/"+LEAGUE+"/team/"+data.results[0].id+"/"; + $.get(secRequestURL).done(function(data, status) { + callback(data); + Cache.setCache(secRequestURL, data); + }); + } + Cache.setCache(requestURL, data); + }).fail(function(xhr, status, error) { + callback(false); + }); + } } static updateTeam(params, callback) { @@ -244,15 +237,16 @@ class Api { static getUserProfile(callback) { var profileURL = URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"; - var data = Api.getCache(profileURL); + var data = Cache.getCache(profileURL); + if (data!==null) { callback(data); - var userURLData = Api.getCache(data.url); + var userURLData = Cache.getCache(data.url); if (userURLData!==null) { callback(userURLData); } else { $.get(data.url).done(function(userURLData, success) { - Api.setCache(data.url, userURLData); + Cache.setCache(data.url, userURLData); callback(userURLData); }).fail(function(xhr, status, error) { console.log(error); @@ -260,11 +254,11 @@ class Api { } } else { $.get(profileURL).done(function(data, status) { - Api.setCache(profileURL, data); + Cache.setCache(profileURL, data); Cookies.set('user_url', data.url); $.get(data.url).done(function(userURLData, success) { - Api.setCache(data.url, userURLData); + Cache.setCache(data.url, userURLData); callback(userURLData); }).fail(function(xhr, status, error) { console.log(error); @@ -274,8 +268,8 @@ class Api { } static updateUser(profile, callback) { - Api.deleteCache(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"); - Api.deleteCache(Cookies.get('user_url')); + Cache.deleteCache(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/"); + Cache.deleteCache(Cookies.get('user_url')); $.ajax({ url:Cookies.get('user_url'), @@ -307,6 +301,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..de9edb4 --- /dev/null +++ b/app/src/cache.js @@ -0,0 +1,36 @@ + +export default class Cache { + static setCache(url, data) { + data['expiry_time'] = (new Date()).getTime() + 300000; + 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 From 066dae1e77a818fa3ba4bac0f85a9311d56b2d93 Mon Sep 17 00:00:00 2001 From: AzureFractal Date: Sat, 20 Oct 2018 13:38:21 -0400 Subject: [PATCH 3/3] Add timeout parameter for setCache --- app/src/cache.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/cache.js b/app/src/cache.js index de9edb4..4380da7 100644 --- a/app/src/cache.js +++ b/app/src/cache.js @@ -1,7 +1,7 @@ export default class Cache { - static setCache(url, data) { - data['expiry_time'] = (new Date()).getTime() + 300000; + 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);