Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/content/docs/durable-objects/reference/in-memory-state.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ Variables in a Durable Object will maintain state as long as your Durable Object
A common pattern is to initialize a Durable Object from [persistent storage](/durable-objects/api/storage-api/) and set instance variables the first time it is accessed. Since future accesses are routed to the same Durable Object, it is then possible to return any initialized values without making further calls to persistent storage.

```js
export class Counter {
constructor(ctx, env) {
this.ctx = ctx;
// `blockConcurrencyWhile()` ensures no requests are delivered until
// initialization completes.
this.ctx.blockConcurrencyWhile(async () => {
let stored = await this.ctx.storage.get("value");
// After initialization, future reads do not need to access storage.
this.value = stored || 0;
});
}

// Handle HTTP requests from clients.
async fetch(request) {
// use this.value rather than storage
}
import { DurableObject } from "cloudflare:workers";

export class Counter extends DurableObject {
constructor(ctx, env) {
super(ctx, env);
// `blockConcurrencyWhile()` ensures no requests are delivered until
// initialization completes.
this.ctx.blockConcurrencyWhile(async () => {
let stored = await this.ctx.storage.get("value");
// After initialization, future reads do not need to access storage.
this.value = stored || 0;
});
}

// Handle HTTP requests from clients.
async fetch(request) {
// use this.value rather than storage
}
}
```

Expand Down
37 changes: 19 additions & 18 deletions src/content/docs/kv/concepts/kv-bindings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ With this, the deployed Worker will have a `TODO` field in their environment obj

```js
export default {
async fetch(request, env, ctx) {
// Get the value for the "to-do:123" key
// NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace.
let value = await env.TODO.get("to-do:123");

// Return the value, as is, for the Response
return new Response(value);
},
async fetch(request, env, ctx) {
// Get the value for the "to-do:123" key
// NOTE: Relies on the `TODO` KV binding that maps to the "My Tasks" namespace.
let value = await env.TODO.get("to-do:123");

// Return the value, as is, for the Response
return new Response(value);
},
};
```

Expand Down Expand Up @@ -83,15 +83,16 @@ kv_namespaces = [
An example might look like:

```js
export class DurableObject {
constructor(ctx, env) {
this.ctx = ctx;
this.env = env;
}

async fetch(request) {
const valueFromKV = await this.env.NAMESPACE.get("someKey");
return new Response(valueFromKV);
}
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
constructor(ctx, env) {
super(ctx, env);
}

async fetch(request) {
const valueFromKV = await this.env.NAMESPACE.get("someKey");
return new Response(valueFromKV);
}
}
```
Loading