Avatar photo

Flexible Client-side Caching

At Keen one of the things we’re focusing on is decreasing “time to first insight” as we want to make it easy for you to deliver lightning-fast embedded analytics.

To do that we’re making sure that you have the ability to accelerate and cache queries at all levels of your Keen embedded analytics stack.

We’re happy to share that keen-analysis.js now supports flexible Client-side Caching. It allows you to cache query results client side in your user’s browser so that follow on or repeat queries get answered fast and efficiently.

It’s easy to implement and flexible feature for you to leverage. You can choose to cache all queries, or just specific queries – and you can of course control how long to cache results for:

[code lang=”js”]
// set global caching of all queries *Optional*
const client = new KeenAnalysis({
projectId: ‘YOUR_PROJECT_ID’,
masterKey: ‘YOUR_MASTER_KEY’,
cache: {
maxAge: 60 * 1000 // cache for 1 minute
}
});

// or set custom caching for a specific query *Optional*
client
.query({
analysis_type: ‘count’,
event_collection: ‘pageviews’,
timeframe: ‘this_31_days’,
cache: {
maxAge: 10 * 1000 // [ms]
}
})
.then(res => {
// Handle results
})
.catch(err => {
// Handle errors
});
[/code]

For more details check out the SDK documentation here.

So when might you want to use Client-side Caching?

It’s most useful in cases where users are latency sensitive or where you want to control how often a user can run a query.

For example, if you had a dashboard that showed “Ad Engagement in the last 7 days” you might want to ensure that you always have the latest data. But, what if you’re also showing “Ad Engagement in the last 12 months”? That’s likely to be a more costly query due to the long time horizon. You could opt to cache that query for 24 hours so it’s not fetched as frequently.

The other scenario where Client-side Caching can come in handy is if you anticipate that you might need to show a user a different set of queries soon. You could run these queries in the background when the user loads your application and then have the results cached and ready should the user need them. This is a great way to provide a user experience where loading other metrics feels instant.

Client-side Caching along with our service side feature’s of Cached Datasets and Cached Queries give you a rich set of features that you can leverage to deliver metrics and insights to your end users with lightning speed.