Skip to main content
Version: 3.11.0

SimpleCache

The SimpleCache class provides a simplified interface to inserting and retrieving entries from Fastly's Cache.

All the methods on the class are static methods, there are no instance methods.

Examples

In this example we attempt to retrieve an entry from the Fastly Cache, if the entry does not exist, we create the content and insert it into the Fastly Cache before finally returning.

/// <reference types="@fastly/js-compute" />

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 = SimpleCache.getOrSet(path, async () => {
return {
value: await render(path),
// Store the page in the cache for 1 minute.
ttl: 60
}
});
return new Response(page, {
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;
}