Android index.enableSearchCache()

Hello everyone,
I can’t figure out the behaviour of an index after calling enableSearchCache() on it.
I would expect that it internally caches the results and uses those if the same search is done.
What I did was calling that function on my index, making a search calling index.searchAsync(…), then disabling the internet connection and performing the same query, but then it finds no results at all.
I searched online for some more specific detail about this function, but I could not find any.
Can anybody help me?
Thank you in advance.

Hi @mattia.dipi, thanks for reaching out.

You’re right that this feature’s documentation is lacking, it seems it has been deleted from the readme in an automated update. I’ll make sure to document it again!

In the meantime, here’s how this feature works:

  • By default the cache is disabled
  • When enabled, by default it caches 64 queries with a delay of 2mn each
  • When a query is sent, the cache will store its response
  • If the same query is sent before the delay, the response is returned without querying again your Algolia index.

You can check the cache tests that confirm this behavior.

Let me know if this helps or if you have further questions!

Thanks for your answer!
But I still can’t figure out how it works.
Are the cached results stored on the device? Or are they stored online and the response is just resent back directly, without having to query the index again?

Hi @mattia.dipi, you’re welcome.

The results are stored in the device, in RAM (the point of this cache is to avoid network load on your user’s side when redoing a query that was resolved recently).

The implementation is pretty straightforward, don’t hesitate to check its code: the index has a searchCache which is a wrapper over the system LRUCache.

Thank you, I finally got what I was doing wrong. I had a index2.getObjectsAsync inside a the completion handler of a index1.searchAsync, and it looks like caching only works while using index.searchAsynch.
Have a nice day!

1 Like

Hey @mattia.dipi, noprob glad I could help!

Indeed the search cache is only applied to search queries.
Would you see value in it caching any Read network operation, such as getObject or getSettings?
We only implemented it for search as we considered caching other calls could be confusing to some customers, but I’m happy to reconsider it if you think it would provide as much value as caching the search queries :slight_smile:

Have a nice day as well!