本文へ移動
章と記事の一覧

動かすストレージ

Durable Objects

名前ごとに 1 つの実体。状態と直列化

エディタへ移動

export default {
  async fetch(request, env) {
    // 同じ名前(lesson-counter)の実体にルーティングされる
    // Routes to the exact same Durable Object instance identified by name
    const before = await env.COUNTER.read();

    const now = await env.COUNTER.increment();
    const again = await env.COUNTER.increment();

    console.log("before =", before);
    console.log("increment ->", now, again);

    return Response.json({
      before,
      after: again,
      // この値は Worker の再起動でも消えない(DO ストレージに永続化。24 時間呼ばれないと削除される)
      // Persists across Worker restarts (saved in Durable Object storage; deleted after 24 hours without a call)
      note: "Durable Object storage",
    });
  },
};

実行結果

実行するとここに結果が出ます。ネットワークは塞がれていますが、D1・KV・R2・Durable Object の状態は訪問者全員で共有されます。