new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Cloudflare Workers · all subjects

kv storage

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Astro Sessions API with KV

Astro's Sessions API allows storing user data between requests using Cloudflare Workers KV. When using the Cloudflare adapter, Astro automatically configures Workers KV for session storage, and Wrangler automatically provisions a KV namespace named SESSION at deploy time.

Astro Sessions API example

To use Astro Sessions with Cloudflare: set export const prerender = false at the top level of the page, then access session data with const cart = await Astro.session?.get("cart");

Customize Astro session KV binding

You can customize the KV binding name for Astro sessions using the sessionKVBindingName adapter option in the Cloudflare adapter configuration if you want to use a different binding name instead of the default SESSION.

Create KV namespace with wrangler command

Use the command 'npx wrangler kv namespace create "NAMESPACE_NAME" --preview' to create a new KV namespace. The --preview flag creates a preview namespace instead of a production namespace. The command returns a namespace ID that must be copied and added to the Wrangler configuration.

Configure KV namespace binding in wrangler.toml

In wrangler.toml, define a kv_namespaces array with objects containing binding name, id, and preview_id fields. Example: [{ "binding": "TODOS", "id": "<YOUR_ID>", "preview_id": "<YOUR_PREVIEW_ID>" }]. This makes the namespace accessible in the Worker code via env.BINDING_NAME.

KV primary methods: get, put, delete

A KV namespace has three primary methods: get() to retrieve a value by key, put() to store a value by key, and delete() to remove a value by key.

KV is eventually consistent within regions

Workers KV is an eventually consistent, global datastore. Writes within a region are immediately reflected within that same region but are not immediately available in other regions. Those writes will eventually be available everywhere, and at that point, Workers KV guarantees data within each region will be consistent.

Store JSON data in KV using JSON.stringify

To store JSON data in KV, use JSON.stringify() to convert the JavaScript object to a string before calling env.KV_NAMESPACE.put(key, stringifiedData). When retrieving, use JSON.parse() to convert the string back to a JavaScript object.

Implement user-specific KV cache keys

To support per-user data, generate cache keys based on client request properties. For example, use const myKey = `data-${ip}` where ip = request.headers.get("CF-Connecting-IP") to create per-user cache keys. Each visitor will then have their own isolated data in the same KV namespace.

Give your agent this brain