SimpleCache

Classin cache@fastly/js-compute 3.46.0View source

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 instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

staticView source
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

static

Overload 1 of 2

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

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

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

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

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

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

staticView source
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

staticdeprecated
Deprecated: Use SimpleCache.getOrSet instead.

Overload 1 of 2

static set(key: string, value: BodyInit, ttl: number): undefined
View source

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

static set(key: string, value: ReadableStream, ttl: number, length: number): undefined
View source

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.