简体   繁体   English

在 Google Cloud Firestore 上存储临时数据(1 分钟后自动删除)

[英]Store temporary data (that automatically deletes after 1 minute) on Google Cloud Firestore

I have an application where users keep writing data to the database that only needs to last for 1 minute.我有一个应用程序,用户不断将数据写入数据库,只需要持续 1 分钟。 For example, Alice stores {foo: bar} at 10:36:00pm, and at 10:37:00pm, that entry should automatically be deleted from the database to free up memory, as it will never be used again.例如,Alice 在晚上 10:36:00 存储{foo: bar} ,在晚上 10:37:00,应该自动从数据库中删除该条目以释放 memory,因为它永远不会被再次使用。

Is there a way to do this, or do I have no choice but to manually have users delete their temp data after a minute?有没有办法做到这一点,或者我别无选择,只能在一分钟后手动让用户删除他们的临时数据? That wouldn't be ideal, because if a user "forgets" to (eg they lose their internet connection or close the app before they delete), then that entry will stay taking up space forever unless I set up a worker manually cleaning the database of these old entries.这并不理想,因为如果用户“忘记”(例如,他们失去了互联网连接或在删除之前关闭了应用程序),那么除非我设置一个工作人员手动清理数据库,否则该条目将永远占用空间这些旧条目。

For the record, the intended use case is to have Firestore act as a WebRTC signalling server, where users temporarily signal each other to establish a p2p connection.作为记录,预期的用例是让 Firestore 充当 WebRTC 信令服务器,用户在其中临时互相发送信号以建立 p2p 连接。

As far as I can tell, there's no such thing in Firestore .据我所知, Firestore 中没有这样的东西 This mechanism is commonly described as TTL (Time To Live).这种机制通常被描述为 TTL(生存时间)。

You can, however, roll your own TTL mechanism using Cloud Tasks .但是,您可以使用 Cloud Tasks 推出自己的 TTL 机制

For your use case, very “short-term”, heavy-rotation, ephemeral, and presumably small data, you may also consider Redis, which has built-in TTL and higher performance.对于您的用例,非常“短期”、大量轮换、短暂且可能很小的数据,您还可以考虑 Redis,它具有内置 TTL和更高的性能。 Depending on your volume, may turn out cheaper as well.根据您的数量,可能会变得更便宜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM