I’m working on a Firebase project with Cloud Functions and Firestore. I want to implement a repository pattern as a singleton that includes an internal cache. The cache should only be in-memory and should not persist between function invocations.
Here’s what I’m aiming for:
- The repository should fetch data from Firestore and store it in the internal cache.
- Subsequent requests within that cloud function call for the same data should return it from the cache instead of making additional reads to Firestore.
- The cached data should be shared across different parts of the code within that cloud function to avoid multiple reads without needing to pass objects through multiple function layers.
For example, consider a “restaurants” collection. My Cloud Functions might need to read one or more restaurant documents and reuse this data in various sub-functions without re-fetching it each time and passing down those “Restaurant” objects.
Is it possible to achieve this?