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:

  • Retrieve the schema from the database again. This solution works but is often not desired since it requires an extra call to the database at query time. The /datasets endpoint is not called frequently, while the /query endpoint is called for every dashboard view.
  • Avoid the extra call and implement a cache. Since the performance of the /query endpoint has a direct impact on the loading time of a dashboard, this is the preferred option.

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
  }
}
Previous
Next

Need more information?

Do you still have questions? Let us know how we can help.
Send us feedback!