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,6 +34,42 @@ angular.module('portainer.services')
LocalStorage.storeApplicationState(state.application); LocalStorage.storeApplicationState(state.application);
}; };
function assignStateFromStatusAndSettings(status, settings) {
state.application.authentication = status.Authentication;
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();
}
function loadApplicationState() {
var deferred = $q.defer();
$q.all({
settings: SettingsService.publicSettings(),
status: StatusService.status()
})
.then(function success(data) {
var status = data.status;
var settings = data.settings;
assignStateFromStatusAndSettings(status, settings);
LocalStorage.storeApplicationState(state.application);
deferred.resolve(state);
})
.catch(function error(err) {
deferred.reject({msg: 'Unable to retrieve server settings and status', err: err});
})
.finally(function final() {
state.loading = false;
});
return deferred.promise;
}
manager.initialize = function () { manager.initialize = function () {
var deferred = $q.defer(); var deferred = $q.defer();
@ -44,32 +80,28 @@ angular.module('portainer.services')
var applicationState = LocalStorage.getApplicationState(); var applicationState = LocalStorage.getApplicationState();
if (applicationState) { if (applicationState) {
state.application = applicationState; var now = moment().unix();
state.loading = false; var cacheValidity = now - applicationState.validity;
deferred.resolve(state); 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 { } else {
$q.all({ loadApplicationState()
settings: SettingsService.publicSettings(),
status: StatusService.status()
})
.then(function success(data) { .then(function success(data) {
var status = data.status;
var settings = data.settings;
state.application.authentication = status.Authentication;
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);
deferred.resolve(state); deferred.resolve(state);
}) })
.catch(function error(err) { .catch(function error(err) {
deferred.reject({msg: 'Unable to retrieve server settings and status', err: err}); deferred.reject(err);
})
.finally(function final() {
state.loading = false;
}); });
} }

Loading…
Cancel
Save