Database Reference
In-Depth Information
locally on the client machine. This way, if the user requests a particular query that
has already been retrieved, we can access the locally cached copy for a very quick user
experience. Listing 6.4 provides an example of using the HTML Web Storage API to
cache results locally. Note that this example does not include any cache expiration or
sophisticated string hashing.
Listing 6.4 Simple BigQuery query example in JavaScript
// A simple hashing function
String.prototype.hash = function(){
var hash = 0, i, char;
if (this.length == 0) return hash;
for (i = 0, l = this.length; i < l; i++) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash |= 0;
}
return hash;
};
function runQuery() {
var query = $('#query').val();
var results;
// Attempt to retrieve our cached query result
cachedResult = localStorage[query.hash()];
if (!cachedResult) {
var request = gapi.client.bigquery.jobs.query({
'projectId': project_id,
'timeoutMs': '30000',
'query': query
);
request.execute(function(response) {
results = response;
localStorage[query.hash()] = JSON.stringify(results);
drawTable(results);
});
// If the cached result exists, return the cached value
} else {
results = JSON.parse(cachedResult);
drawTable(results);
}
}
 
 
Search WWH ::




Custom Search