Often, when implementing the /query endpoints, it is necessary to know the type (Luzmo type or database type) of the column that is used in the query. Since the /datasets and /query endpoint are two different calls, we have no access to the result of the /datasets endpoint. However, there are two solutions:
The PostgreSQL plugin implements a cache as follows. It calculates a hash of the properties that uniquely identify the /datasets call. In this case, these properties are host, key and token. The implementation of the /query endpoint will try to get the schema from the cache before it retrieves the schema from the database again.
const TTL = 5 * 60 * 1000
class SchemaCache {
constructor() {
this.cache = {}
this.storeDatasets = this.storeDatasets.bind( this )
this.getDatasets = this.getDatasets.bind( this )
}
// Store in cache
async storeDatasets( details, datasetHashMap ) {
const schemaHash = hash({
database: details.database,
host: details.host,
key: details.key,
token: details.token,
port: details.port })
this.cache[ schemaHash ] = { timestamp: new Date().getTime(), data: datasetHashMap }
}
// Fetch the datasets from the cache
async getDatasets( details ) {
const schemaHash = hash({
database: details.database,
host: details.host,
key: details.key,
token: details.token,
port: details.port })
if ( !this.cache[ schemaHash ] || ( new Date().getTime() - this.cache[ schemaHash ].timestamp > TTL )) {
delete this.cache[ schemaHash ]
return false
}
return this.cache[ schemaHash ].data
}
}