fix(cache): add a cache validity mechanism (#1527)

pull/1528/head
Anthony Lapenna 7 years ago committed by GitHub
parent 8e40eb1844
commit e0b09f20b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,4 +11,5 @@ angular.module('portainer')
.constant('API_ENDPOINT_TEAM_MEMBERSHIPS', 'api/team_memberships') .constant('API_ENDPOINT_TEAM_MEMBERSHIPS', 'api/team_memberships')
.constant('API_ENDPOINT_TEMPLATES', 'api/templates') .constant('API_ENDPOINT_TEMPLATES', 'api/templates')
.constant('DEFAULT_TEMPLATES_URL', 'https://raw.githubusercontent.com/portainer/templates/master/templates.json') .constant('DEFAULT_TEMPLATES_URL', 'https://raw.githubusercontent.com/portainer/templates/master/templates.json')
.constant('PAGINATION_MAX_ITEMS', 10); .constant('PAGINATION_MAX_ITEMS', 10)
.constant('APPLICATION_CACHE_VALIDITY', 3600);

@ -1,5 +1,5 @@
angular.module('portainer.services') angular.module('portainer.services')
.factory('StateManager', ['$q', 'SystemService', 'InfoHelper', 'LocalStorage', 'SettingsService', 'StatusService', function StateManagerFactory($q, SystemService, InfoHelper, LocalStorage, SettingsService, StatusService) { .factory('StateManager', ['$q', 'SystemService', 'InfoHelper', 'LocalStorage', 'SettingsService', 'StatusService', 'APPLICATION_CACHE_VALIDITY', function StateManagerFactory($q, SystemService, InfoHelper, LocalStorage, SettingsService, StatusService, APPLICATION_CACHE_VALIDITY) {
'use strict'; 'use strict';
var manager = {}; var manager = {};
@ -34,20 +34,21 @@ angular.module('portainer.services')
LocalStorage.storeApplicationState(state.application); LocalStorage.storeApplicationState(state.application);
}; };
manager.initialize = function () {
var deferred = $q.defer();
var endpointState = LocalStorage.getEndpointState(); function assignStateFromStatusAndSettings(status, settings) {
if (endpointState) { state.application.authentication = status.Authentication;
state.endpoint = endpointState; state.application.analytics = status.Analytics;
state.application.endpointManagement = status.EndpointManagement;
state.application.version = status.Version;
state.application.logo = settings.LogoURL;
state.application.displayDonationHeader = settings.DisplayDonationHeader;
state.application.displayExternalContributors = settings.DisplayExternalContributors;
state.application.validity = moment().unix();
} }
var applicationState = LocalStorage.getApplicationState(); function loadApplicationState() {
if (applicationState) { var deferred = $q.defer();
state.application = applicationState;
state.loading = false;
deferred.resolve(state);
} else {
$q.all({ $q.all({
settings: SettingsService.publicSettings(), settings: SettingsService.publicSettings(),
status: StatusService.status() status: StatusService.status()
@ -55,13 +56,7 @@ angular.module('portainer.services')
.then(function success(data) { .then(function success(data) {
var status = data.status; var status = data.status;
var settings = data.settings; var settings = data.settings;
state.application.authentication = status.Authentication; assignStateFromStatusAndSettings(status, settings);
state.application.analytics = status.Analytics;
state.application.endpointManagement = status.EndpointManagement;
state.application.version = status.Version;
state.application.logo = settings.LogoURL;
state.application.displayDonationHeader = settings.DisplayDonationHeader;
state.application.displayExternalContributors = settings.DisplayExternalContributors;
LocalStorage.storeApplicationState(state.application); LocalStorage.storeApplicationState(state.application);
deferred.resolve(state); deferred.resolve(state);
}) })
@ -71,6 +66,43 @@ angular.module('portainer.services')
.finally(function final() { .finally(function final() {
state.loading = false; state.loading = false;
}); });
return deferred.promise;
}
manager.initialize = function () {
var deferred = $q.defer();
var endpointState = LocalStorage.getEndpointState();
if (endpointState) {
state.endpoint = endpointState;
}
var applicationState = LocalStorage.getApplicationState();
if (applicationState) {
var now = moment().unix();
var cacheValidity = now - applicationState.validity;
if (cacheValidity > APPLICATION_CACHE_VALIDITY) {
loadApplicationState()
.then(function success(data) {
deferred.resolve(state);
})
.catch(function error(err) {
deferred.reject(err);
});
} else {
state.application = applicationState;
state.loading = false;
deferred.resolve(state);
}
} else {
loadApplicationState()
.then(function success(data) {
deferred.resolve(state);
})
.catch(function error(err) {
deferred.reject(err);
});
} }
return deferred.promise; return deferred.promise;

Loading…
Cancel
Save