您如何使用 Google Cloud 运行 缓存数据库查询?

How do you cache database queries with Google Cloud Run?

我在 GCR 上有一个服务器,它在调用时会 ping 一个数据库。我正在考虑添加一个简单的缓存机制,比如

var lastDBUpdate int
var lastCache int
if lastDBUpdate > lastCache {
  lastCache = now
  return newResults
} else {
  return cachedResults
}
// endpoints that modify the db update the lastDBUpdate global var

如果只有一个容器(即我的后端负载很小),这会起作用,但随着我的应用程序的增长和创建多个容器,lastDBUpdate 和 lastCache 变量将在不同的容器之间不同步。那么如何使用 GCR 缓存数据库读取?

您可以使用 Memorystore。

这里是 guide 如何从云端连接到 Redis 实例 运行。