> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bunny.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Reading & writing

> Reading from, writing to, and deleting entries on a Cache instance.

## Cache-Control behavior

Cache entry lifetimes are controlled via the `Cache-Control` header on the response you pass to `put()`. The same directives that govern HTTP caching apply here. For the full directive list, see [Cache-Control on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control).

```typescript theme={null}
await cache.put(
  `${url.origin}/example.html`,
  new Response("content", {
    headers: { "Cache-Control": "max-age=60" },
  }),
);
```

This caches the string `content` under the key `/example.html` for 60 seconds.

## Instance Methods

### Delete

The `delete()` method finds the Cache entry whose key matches the request and removes it, returning `true` if an entry was found and deleted, or `false` if there was no matching entry.

#### Syntax

```typescript theme={null}
await cache.delete(request);
```

Unlike the browser Cache API, Bunny Edge Scripting does not support the options bag (`ignoreSearch`, `ignoreMethod`, `ignoreVary`). To get equivalent behaviour, normalize the key at `put()` time — e.g. strip query strings from the URL before calling `put()`.

#### Parameters

`request`
The Request you are looking to delete. This can be a Request object or a URL.

#### Return Value

A `Promise` that resolves to `true` if a matching entry was found and removed, or `false` if no entry existed. After the promise resolves, a subsequent `cache.match()` for the same key will miss.

#### Exceptions

Can throw an exception if there is an issue accessing the underlying cache storage.

#### Example

```typescript theme={null}
const cache = await caches.open("cache:v1");
const deleted = await cache.delete(`${url.origin}/example.html`);
```

See [Refresh and purge a cached value on demand](./examples#refresh-and-purge-a-cached-value-on-demand) for a full request handler that exposes `DELETE` as a purge endpoint.

### Match

The `match()` method of the Cache returns a Promise that resolves to the Response associated with the first matching request in the Cache object. If no match is found, the Promise resolves to `undefined`.

#### Syntax

```typescript theme={null}
await cache.match(request);
```

Unlike the browser Cache API, Bunny Edge Scripting does not support the options bag (`ignoreSearch`, `ignoreMethod`, `ignoreVary`). You can accomplish equivalent behaviour by removing query strings or unwanted headers from the key at `put()` time.

#### Parameters

`request`
The Request for which you are attempting to find responses in the Cache. This can be a Request object or a URL.

#### Return Value

A `Promise` that resolves to the first `Response` that matches the request or to `undefined` if no match is found.

#### Exceptions

Can throw an exception if there is an issue accessing the underlying cache storage.

#### Example

```typescript theme={null}
const cache = await caches.open("cache:v1");
const hit = await cache.match(`${url.origin}/example.html`);
if (hit) return hit;
```

See [Quickstart](./examples#quickstart) for a full handler that pairs a `match()` lookup with an `HTMLRewriter` transform.

### Put

The `put()` method of the Cache allows key/value pairs to be added to the current Cache object.

#### Syntax

```typescript theme={null}
await cache.put(request, response);
```

#### Parameters

`request`
The Request object or URL that you want to add to the cache.

`response`
The Response you want to match up to the request.

#### Return Value

A `Promise` that resolves with `undefined`.

#### Exceptions

Can throw an exception if there is an issue accessing the underlying cache storage.

#### Example

```typescript theme={null}
const cache = await caches.open("cache:v1");
// clone() so the response body can still be returned to the caller.
await cache.put(`${url.origin}/example.html`, response.clone());
```

See [Updating a cache in middleware](./examples#updating-a-cache-in-middleware) for a full handler that populates the cache in response to origin requests.

## References

* [Examples](./examples) — end-to-end recipes: cache-aside, middleware writes, refresh and purge.
* [Managing caches](./managing-caches) — the global `caches` object and named cache instances.
* [Cache API overview](./index) — regional behavior, limitations, and API surface.
