|
| 1 | +--- |
| 2 | +type: example |
| 3 | +summary: Implement a Time To Live (TTL) for Durable Object instances. |
| 4 | +tags: |
| 5 | + - Durable Objects |
| 6 | + - Alarms |
| 7 | + - API |
| 8 | +pcx_content_type: example |
| 9 | +title: Durable Object Time To Live |
| 10 | +sidebar: |
| 11 | + order: 4 |
| 12 | +description: Use the Durable Objects Alarms API to implement a Time To Live (TTL) for Durable Object instances. |
| 13 | +--- |
| 14 | + |
| 15 | +import { TabItem, Tabs, GlossaryTooltip } from "~/components"; |
| 16 | + |
| 17 | +A common feature request for Durable Objects is a Time To Live (TTL) for Durable Object instances. Durable Objects give developers the tools to implement a custom TTL in only a few lines of code. This example demonstrates how to implement a TTL making use of <GlossaryTooltip term="alarm">`alarms`</GlossaryTooltip>. While this TTL will be extended upon every new request to the Durable Object, this can be customized based on a particular use case. |
| 18 | + |
| 19 | +<Tabs> <TabItem label="JavaScript" icon="seti:javascript"> |
| 20 | + |
| 21 | +```js |
| 22 | +import { DurableObject } from "cloudflare:workers"; |
| 23 | + |
| 24 | +// Durable Object |
| 25 | +export class MyDurableObject extends DurableObject { |
| 26 | + // Time To Live (TTL) in milliseconds |
| 27 | + timeToLiveMs = 1000; |
| 28 | + |
| 29 | + constructor(ctx, env) { |
| 30 | + super(ctx, env); |
| 31 | + |
| 32 | + this.ctx.blockConcurrencyWhile(async () => { |
| 33 | + await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + async fetch(_request) { |
| 38 | + // Increment the TTL immediately following every request to a Durable Object |
| 39 | + await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs); |
| 40 | + ... |
| 41 | + } |
| 42 | + |
| 43 | + async alarm() { |
| 44 | + await this.ctx.storage.deleteAll(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Worker |
| 49 | +export default { |
| 50 | + async fetch(request, env) { |
| 51 | + const id = env.MY_DURABLE_OBJECT.idFromName("foo"); |
| 52 | + const stub = env.MY_DURABLE_OBJECT.get(id) |
| 53 | + return await stub.fetch(request); |
| 54 | + }, |
| 55 | +}; |
| 56 | +``` |
| 57 | + |
| 58 | +</TabItem> <TabItem label="TypeScript" icon="seti:typescript"> |
| 59 | + |
| 60 | +```ts |
| 61 | +import { DurableObject } from "cloudflare:workers"; |
| 62 | + |
| 63 | +export interface Env { |
| 64 | + MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObject>; |
| 65 | +} |
| 66 | + |
| 67 | +// Durable Object |
| 68 | +export class MyDurableObject extends DurableObject { |
| 69 | + // Time To Live (TTL) in milliseconds |
| 70 | + timeToLiveMs = 1000; |
| 71 | + |
| 72 | + constructor(ctx: DurableObjectState, env: Env) { |
| 73 | + super(ctx, env); |
| 74 | + |
| 75 | + this.ctx.blockConcurrencyWhile(async () => { |
| 76 | + await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + async fetch(_request: Request) { |
| 81 | + // Increment the TTL immediately following every request to a Durable Object |
| 82 | + await this.ctx.storage.setAlarm(Date.now() + this.timeToLiveMs); |
| 83 | + ... |
| 84 | + } |
| 85 | + |
| 86 | + async alarm() { |
| 87 | + await this.ctx.storage.deleteAll(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Worker |
| 92 | +export default { |
| 93 | + async fetch(request, env) { |
| 94 | + const id = env.MY_DURABLE_OBJECT.idFromName("foo"); |
| 95 | + const stub = env.MY_DURABLE_OBJECT.get(id) |
| 96 | + return await stub.fetch(request); |
| 97 | + }, |
| 98 | +} satisfies ExportedHandler<Env>; |
| 99 | +``` |
| 100 | + |
| 101 | +</TabItem> </Tabs> |
| 102 | + |
| 103 | +To test and deploy this example, configure your `wrangler.toml` file to include a Durable Object [binding](/durable-objects/get-started/walkthrough/#5-configure-durable-object-bindings) and [migration](/durable-objects/reference/durable-objects-migrations/) based on the namespace and class name chosen previously. |
| 104 | + |
| 105 | +import { WranglerConfig } from "~/components"; |
| 106 | + |
| 107 | +<WranglerConfig> |
| 108 | + |
| 109 | +```toml title="wrangler.toml" |
| 110 | +name = "durable-object-ttl" |
| 111 | + |
| 112 | +[[durable_objects.bindings]] |
| 113 | +name = "MY_DURABLE_OBJECT" |
| 114 | +class_name = "MyDurableObject" |
| 115 | + |
| 116 | +[[migrations]] |
| 117 | +tag = "v1" |
| 118 | +new_classes = ["MyDurableObject"] |
| 119 | +``` |
| 120 | + |
| 121 | +</WranglerConfig> |
0 commit comments