Maintain a client-side cache of preference values, populated using an async call. Fixes #2487

This commit is contained in:
Khushboo Vashi 2017-06-21 17:09:59 +01:00 committed by Dave Page
parent b9f6fb9545
commit 67fd119de8
4 changed files with 63 additions and 28 deletions

View File

@ -325,6 +325,9 @@ define(
}
obj.initialized = true;
// Cache preferences
obj.cache_preferences();
// Initialize the Docker
obj.docker = new wcDocker(
'#dockerContainer', {
@ -597,7 +600,7 @@ define(
// This will hold preference data (Works as a cache object)
// Here node will be a key and it's preference data will be value
node_preference_data: {},
preferences_cache: {},
// Add menus of module/extension at appropriate menu
add_menus: function(menus) {
@ -749,24 +752,42 @@ define(
}
},
get_preference: function (module, preference_name) {
var preference = null;
// Get preference value from cache
get_preference: function(module, preference) {
var self = this;
// If cache is not yet loaded then keep checking
if(_.size(self.preferences_cache) == 0) {
var preference_data = setInterval(check_preference, 1000);
function check_preference() {
if(_.size(self.preferences_cache) > 0) {
clearInterval(preference_data);
return _.findWhere(self.preferences_cache, {'module': module, 'name': preference});
}
}
}
else {
return _.findWhere(self.preferences_cache, {'module': module, 'name': preference});
}
},
// Get and cache the preferences
cache_preferences: function () {
var self = this;
$.ajax({
async: false,
url: url_for(
'preferences.get_by_name', {
'module': module,
'preference': preference_name
}),
url: url_for('preferences.get_all'),
success: function(res) {
preference = res;
self.preferences_cache = res;
},
error: function(xhr, status, error) {
try {
var err = $.parseJSON(xhr.responseText);
Alertify.alert(gettext('Preference loading failed.'),
err.errormsg
);
} catch (e) {}
}
});
return preference;
},
_findTreeChildNode: function(_i, _d, _o) {

View File

@ -60,7 +60,7 @@ class PreferencesModule(PgAdminModule):
Returns:
list: a list of url endpoints exposed to the client.
"""
return ['preferences.index', 'preferences.get_by_name']
return ['preferences.index', 'preferences.get_by_name', 'preferences.get_all']
blueprint = PreferencesModule(MODULE_NAME, __name__)
@ -142,6 +142,27 @@ def preferences(module=None, preference=None):
)
@blueprint.route("/get_all", methods=["GET"], endpoint='get_all')
@login_required
def preferences_s():
"""Fetch all preferences for caching."""
# Load Preferences
pref = Preferences.preferences()
res = []
for m in pref:
if len(m['categories']):
for c in m['categories']:
for p in c['preferences']:
p['module'] = m['label']
res.append(p)
return ajax_response(
response=res,
status=200
)
@blueprint.route("/<int:pid>", methods=["PUT"], endpoint="update")
@login_required
def save(pid):

View File

@ -389,6 +389,8 @@ define([
if (e.button.text == gettext('OK')){
preferences.updateAll();
// Refresh preferences cache
pgBrowser.cache_preferences();
}
},
build: function() {

View File

@ -30,21 +30,12 @@ define(['jquery', 'underscore', 'underscore.string'],
return true;
}
// If we have already fetched preference earlier then pick
// it from our cache object
if (_.has(pgBrowser.node_preference_data, node_type)) {
return pgBrowser.node_preference_data[node_type].value
preference = pgBrowser.get_preference("Browser", 'show_node_'+node_type);
if (preference) {
return preference.value
}
var preference = pgBrowser.get_preference(
'browser', 'show_node_' + node_type
);
// Save it for future use, kind of caching
if(!_.isUndefined(preference) && !_.isNull(preference)) {
pgBrowser.node_preference_data[node_type] = preference;
return preference.value;
} else {
else {
return true;
}
}