Alex
Alex

Reputation: 4264

Attach firebase cloud function or cache its data from cloud function call

I have a frontend component that consists of a chart and several different filters that allow users to filter by data type. However, the data that they are filtering is relatively large, and so I do not want to load all of it into the webpage, and instead have a firebase cloud function handle the filtering. The issue is that users will usually do a bunch of filtering while using this component, so it does not make sense for the cloud function to repeatedly download the necessary data. Is there a way to "attach" the cloud function to the call and have it update without having to re-retrieve the data, or to somehow cache the retrieved firebase data somewhere accessible to the cloud function if this is not possible?

exports.handleChartData = functions.database.ref("chartData").onCall((data, context) => {
    // can I cache data here somehow
    // or can I have this function read in updates from user selected filters
    // without having to retrieve data again?
}

Upvotes: 0

Views: 853

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

You can write data to the local /tmp disk. Just be aware that:

  • There is no guarantee that the data will be there next time, as instances are spun up and down as needed. So you will need to check if the file exists on each call, and be ready to create it when it doesn't exist yet.
  • The /tmp disk space is a RAM disk, so any files written there will come out of the memory you've allocated for your Cloud Functions containers.
  • You can't reliably keep listeners alive across calls, so you won't be able to update the cache.

Also see:

Upvotes: 3

Related Questions