Can I use Env for caching database results?
The HelloWorld worker example recommends using the
Env
object to store the router
which is initialized once and never modified:
My API depends on some database values (policies
) which are very rarely changed, but when they do, they need to be respected immediately and everywhere. I thought of using Env
for this as well and modified the above to:
and I defined a /refresh
API that I will call explicitly when policies change in db like this:
This seems to work fine locally. But I am not sure if this will work correctly in production. Given that the worker isolates are deployed at multiple locations across the globe, my /refresh
API will only reach one of the worker instances and all other instances will continue to have stale data in their env.policies
.
Is there a way to cache DB values while able to invalidate them globally at once?1 Reply
you correctly answered that this will not work as you wish, you need to check
Durable Objects
for this use-case