Query Cache JayData Pro module

13 Oct
Categories: Featured
Author: bonayr
* * * * *

Caching is extremely important if you want to save network payload or improve the response time of your app. The JayData Query Cache module helps you to achieve this in your HTML5/JavaScript/PhoneGap/node.js app.

The query cache module is shipped with JayData Pro, cannot be found in the open-source JayData package, although you can try it free for 30 days after downloading the JayData Pro Trial version.

This extension can be useful if you want to cache the responses to each queries in your JavaScript application. It saves the results to the memory and returns these entities if it detects the same query with the same parameters in the future.

We will go through all the features of Query Cache module to explain how can you reduce the network traffic and the response time in your project.


Key features:

  • Explicit read from cache
  • Explicit read without the cache
  • Manual cache cleanup
  • Automatic cache cleanup after each saveChanges()
  • Timeout-based cache invalidation
  • Context-level cache timeout
  • Global cache limit to reduce memory consumption of mobile devices



The Query Cache module is compatible with the following JayData providers:

  • OData
  • Web API
  • WebSQL/SQLite
  • WebSQL/SQLite Provider Pro
  • IndexedDB
  • IndexeDB Provider Pro
  • MongoDB, MongoDB Pro
  • HTML5 localStorage



Caching data with JayData Query Cache module

Everything begins with loading the module after jaydata.js:

<script src="jaydata.js"></script>
<script src="/jaydatamodules/QueryCache.js"></script>


After loading the module, it can be initialized during all context creations to turn the caching on.

You can create you EntityContext instance - with any kind of supported providers – with a specified timeout interval by setting the queryCache parameter.

var providerConfig = {
        name : 'oData',
        oDataServiceHost: 'http://yourServiceUrl',
        queryCache: 500
var db = new NorthwindContext(providerConfig);


Now all the queries of the db context will be cached for 500 milliseconds.

    .filter(function (it) {
        return it.DisplayName.startsWith('Thai')
    .toArray(function (result) {
        // result is cached for this query because of the context-level configuration


During the execution of the query with this filter above, the module saves the result to the memory, and for the same query – and same parameters – it will return the saved results from the memory without hitting the WebSQL/SQLite/MongoDB/IndexeDB database or performing OData/WebAPI requests.

You can set true to the context-level queryCache parameter, which will cause keeping the query results until a manual cache cleanup.


var providerConfig = {
    queryCache: true




You can cache even mapped objects, which are retrieved by the projection of a field subset:


    .map(function(p)  {return {Name: p.Name} })
    .toArray(function () { ...});



Avoiding the cache for a query

the module extends the standard JavaScript Language Query (JSLQ) operator set with the withCache() and noCache() frame operators, you have more control over your queries. Loading the entities by bypassing the cache is a typical developer requirement to make sure you have the up-to-date results. You can achieve this using the noCache() and the withCache(false) frame operators on your query. There is no difference, the noCache() is just an alias, you can use them according to your preference.

.filter('it.ProductID == 40')
.toArray(function (result) {...});

.filter('it.ProductID == 40')
.toArray(function (result) {...});


This feature can be useful is you work in a team and you want to be independent from the cache settings in other developers’ code.


Writing cached queries from non-cached contexts

Assuming you created a context without setting the queryCache, you can turn on this for a specific query without specifying the timeout interval by adding the withCache() or withCache(true) operators.

db.Products.filter(function (it) {
    return it.DisplayName.startsWith('Thai')
.toArray(function () { ... });


The withCache() and withCache(true) adds the result of the query to the cache until the manual invalidation, but you can also override context cache setting with query-level cache using a query-level timeout interval.



Manual cache cleanup

The cache cleanup can be invoked on the EntityContext, which will remove all the previous results from the memory:



Automatic cache cleanup after each saveChanges()

Every call to db.saveChanges() causes a full cache cleanup to avoid possible outdated dependencies.



Manual time-out based invalidation

You can tell JayData to cache the results of your query only for a specific time (in milliseconds), this setting overrides the timeout interval of the context – and turns on the caching for this query even if the context was configured to use no cache.

db.Products.filter(function (it) {
    return it.DisplayName.startsWith('Thai')
.toArray(function () { ... });



Specifying the timeout interval globally

We have seen how to control the timeout interval on the context using the queryCache parameter, but you can define this timeout interval globally, too.

$data.defaults.queryCache = 2000;

The value passed here will be used in all the initialized contexts, so you won’t have to to initialize contexts or write queries with cache timeouts, because this default global value will be used every time.

Please note that the queryCache parameter of the context overrides this value. As you have seen there are three query timeout configuration options, the precedence order is the following (the highest one is at the beginning):

  1. Query-level – withCache(1000)
  2. Context-level – var context = new Northwind({queryCache: 2000, …});
  3. Global-level – $data.defaults.queryCache



Global cache limit

Limiting the maximum number of cached queries is important to control the memory consumption in mobile devices, you can do this using the global $data.defaults.maxQueryCacheSize.

$data.defaults.maxQueryCacheSize = 3;

This is the maximum total number of queries without explicit timeout interval.



Now you learnt the capabilities of the Query Cache module, give it a try if you liked it, download JayData Pro Trial.

JayData Pro; Query cache

Leave a comment
comments powered by Disqus