How does javascript query caching work?

In a javascript query, there is an advanced option to cache the results of the query and a ttl. Can someone explain how a cache hit is established for js queries? I ask, because I have a particular js query that never hits. The ttl is 300 seconds and I’m requesting an identical query within 15 seconds. The query uses additional scope passed in, which is a js primitive itself. The query uses a salesforce query, but has no reference to any component or anything else external to the js query.

Thanks,

Larry

1 Like
  1. Check your inputs
    The cache considers all inputs (parameters, additionalScope, etc.). Even small differences like undefined vs nullwill cause a cache miss.
// Bad: object creates a new reference each time
query.run({ userId: { id: 123 } });

// Good: use a primitive
query.run({ userId: 123 });


  1. Avoid dynamic objects inside the query
    Objects, arrays, or timestamps created inside the query make it “look different” each run. Precompute values outside if possible.
// Bad
const filter = { type: 'A', ts: Date.now() };

// Good
query.run({ filterType: 'A' });


  1. Keep the query code static
    Don’t use random values or changing global state inside the query. Only use stable inputs.

  2. TTL is fine
    With TTL = 300s, identical inputs within that period should hit the cache.

  3. Debugging tip
    Log inputs to see what’s actually running:

console.log('Query input:', additionalScope);


Run the query twice with identical inputs—if caching works, the second run will be much faster.

@Vishal1620 Thank you for putting the time and effort into your response. All of it makes sense. Most of it I had already considered and checked for. One thing I overlooking, were some logging calls to window.log.debug(…) (obviously custom code). I removed those, and still, no caching.

FWIW – I created a small js query that just returns 1. Not passing anything into it. I setup caching. And even repeated calls to that are never shown as a “cache hit”.

Hi @lkiss, query caching is actually not implemented for JS queries at the moment!

We have a feature request open to grey this option out but on a longer timeframe we will work towards enabling caching for JS queries. I went ahead and bumped this on our internal ticket and will let you know with any updates.