SimpleCache
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.
1234567891011121314151617181920212223import { 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
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
The cached entry, or null if the key does not exist in the cache.
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
Overload 1 of 2
static getOrSet(key: string, set: () => Promise<{ value: BodyInit; ttl: number }>): Promise<SimpleCacheEntry>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
valueandttl(in seconds).
Returns
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.TypeErrorif the providedttlcannot be coerced to a number, is negative,NaN, orInfinity.
Overload 2 of 2
static getOrSet(key: string, set: () => Promise<{ value: ReadableStream; ttl: number; length: number }>): Promise<SimpleCacheEntry>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), andlength(in bytes).
Returns
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.TypeErrorif the providedttlcannot be coerced to a number, is negative,NaN, orInfinity.
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
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
Overload 1 of 2
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
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.
Overload 2 of 2
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
ReadableStreamto store in the cache.- ttl: number
The time-to-live for this entry, in seconds.
- length: number
The length of the
ReadableStreamvalue, in bytes.
Returns
Throws
TypeErrorif the providedkeyis an empty string, cannot be coerced to a string, or is longer than 8,135 characters.