Issue #640
async function useCache(
collectionName: string,
key: string,
response: functions.Response<any>,
fetch: () => Promise<any>
): Promise<any> {
const existing = await db.collection(collectionName).doc(key).get()
if (existing.exists) {
response.send(existing.data())
return
}
const object = await fetch()
const json = Object.assign({}, object)
await db.collection(collectionName).doc(key).set(json)
response.send(object)
}
useCache(
"books",
key,
response,
async () => {
const service = new Service()
return await service.doSomething(key)
}
)