Skip to main content
Version: 3.27.2

SimpleCache.get

get(): string

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

Syntax

get(key)

Parameters

  • key : string
    • The key to retrieve from within the cache.

Return value

If the key does not exist in the cache, this returns null.

If the key does exist in the cache, this returns a SimpleCacheEntry.

Exceptions

  • TypeError
    • If the provided key:
      • Is an empty string
      • Cannot be coerced to a string
      • Is longer than 8135 characters

Examples

In this example we attempt to retrieve an entry from the Fastly Cache, and return a message stating whether the entry was in the Fastly Cache or not.

/// <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.get(path);
return new Response(page ? `${path} is in the cache` : `${path} is not in the cache`, {
headers: {
'content-type': 'text/plain;charset=UTF-8'
}
});
}