---
title: SimpleCache
summary: The SimpleCache class provides a simplified interface to inserting and retrieving entries from Fastly's Cache. All methods on the class are static methods.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache/class.SimpleCache/
---

**Kind:** Class  
**Module:** [cache](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache/)  
**Package:** @fastly/js-compute 4.0.1  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L46

The `SimpleCache` class provides a simplified interface to inserting and retrieving entries
from Fastly's Cache. All methods on the class are static methods.

For more advanced use cases requiring transactional lookups, request collapsing, or
revalidation, use [CoreCache](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache/class.CoreCache/) instead.

```js
import { SimpleCache } from 'fastly:cache';

addEventListener('fetch', event => event.respondWith(app(event)));

async function app(event) {
  const path = new URL(event.request.url).pathname;
  let page = await SimpleCache.getOrSet(path, async () => {
    return {
      value: await render(path),
      // Store the page in the cache for 1 minute
      ttl: 60 // seconds
    }
  });
  return new Response(await page.text(), {
    headers: { 'content-type': 'text/plain;charset=UTF-8' }
  });
}

async function render(path) {
  // expensive/slow function which constructs and returns the contents for a given path
  await new Promise(resolve => setTimeout(resolve, 10_000));
  return path;
}
```

## Methods

### get

**Flags:** static  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L54

```javascript
static get(key: string): SimpleCacheEntry | null
```

Gets the entry associated with the key `key` from the cache.

**Parameters**

- `key`: `string` — The key to retrieve from within the cache (up to 8,135 characters).

**Returns**

`SimpleCacheEntry | null`

The cached entry, or `null` if the key does not exist in the cache.

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.

### getOrSet

**Flags:** static

_Overload 1 of 2_

```javascript
static getOrSet(key: string, set: () => Promise<{ value: BodyInit; ttl: number }>): Promise<SimpleCacheEntry>
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L91

Attempts to get an entry from the cache for the supplied `key`. If no entry is found
(or has expired), the supplied `set` function is executed and its result is inserted
into the cache under the supplied `key`.

**Parameters**

- `key`: `string` — The key to lookup and/or store the entry under (up to 8,135 characters).
- `set`: `() => Promise<{ value: BodyInit; ttl: number }>` — A function to execute if the cache does not have a usable entry. Should return a Promise resolving with `value` and `ttl` (in seconds).

**Returns**

`Promise<SimpleCacheEntry>`

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
- `TypeError` if the provided `ttl` cannot be coerced to a number, is negative, `NaN`, or `Infinity`.

_Overload 2 of 2_

```javascript
static getOrSet(key: string, set: () => Promise<{ value: ReadableStream; ttl: number; length: number }>): Promise<SimpleCacheEntry>
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L107

Attempts to get an entry from the cache for the supplied `key`. If no entry is found
(or has expired), the supplied `set` function is executed and its result is inserted
into the cache under the supplied `key`.

When the value is a `ReadableStream`, the `length` property must be provided.

**Parameters**

- `key`: `string` — The key to lookup and/or store the entry under (up to 8,135 characters).
- `set`: `() => Promise<{ value: ReadableStream; ttl: number; length: number }>` — A function to execute if the cache does not have a usable entry. Should return a Promise resolving with `value`, `ttl` (in seconds), and `length` (in bytes).

**Returns**

`Promise<SimpleCacheEntry>`

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
- `TypeError` if the provided `ttl` cannot be coerced to a number, is negative, `NaN`, or `Infinity`.

### purge

**Flags:** static  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L122

```javascript
static purge(key: string, options: PurgeOptions): undefined
```

Purges the entry associated with the key `key` from the cache.

**Parameters**

- `key`: `string` — The key to purge from within the cache (up to 8,135 characters).
- `options`: `PurgeOptions` — Options controlling the scope of the purge.

**Returns**

`undefined`

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.

### set

**Flags:** static, deprecated

> **DEPRECATED:** Use SimpleCache.getOrSet instead.

_Overload 1 of 2_

```javascript
static set(key: string, value: BodyInit, ttl: number): undefined
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L64

Inserts a new entry or overwrites an existing entry in the cache.

**Parameters**

- `key`: `string` — The key to store the entry under (up to 8,135 characters).
- `value`: `BodyInit` — The value to store in the cache.
- `ttl`: `number` — The time-to-live for this entry, in seconds.

**Returns**

`undefined`

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.

_Overload 2 of 2_

```javascript
static set(key: string, value: ReadableStream, ttl: number, length: number): undefined
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache.d.ts#L75

Inserts a new entry or overwrites an existing entry in the cache using a `ReadableStream`.

**Parameters**

- `key`: `string` — The key to store the entry under (up to 8,135 characters).
- `value`: `ReadableStream` — A `ReadableStream` to store in the cache.
- `ttl`: `number` — The time-to-live for this entry, in seconds.
- `length`: `number` — The length of the `ReadableStream` value, in bytes.

**Returns**

`undefined`

**Throws**

- `TypeError` if the provided `key` is an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
